Skip to content

AddFields Operation

AddFieldsOperation đại diện cho toán tử $addFields của mongoDB. Nó có các method giúp bạn khởi tạo stage $addFields thay vì phải viết BSON thủ công.

import { AddFieldsOperation } from 'red-aggregation/operations';

Phương thức

AddFieldsOperation.addField()

const operation = AddFieldsOperation.addField('name', 'John');
const doc = operation.toDocument(context);

// => { $addFields: { name: "John" } }

Hoặc:

const operation = AddFieldsOperation.addField('fullName', Fields.field('firstName'));
const doc = operation.toDocument(context);

// => { $addFields: { fullName: "$firstName" } }

Hoặc:

const operation = AddFieldsOperation.addField('total', ArithmeticOperators.add('$price').add('$tax'));
const doc = operation.toDocument(context);

// => { $addFields: { total: { $add: ["$price", "$tax"] } } }

AddFieldsOperation.builder()

const operation = AddFieldsOperation.builder()
    .addField("name").withValue("John")
    .addField("age").withValue(25)
    .build();

const doc = operation.toDocument(context);

// => { $addFields: { name: "$John", age: 25 } }
const operation = AddFieldsOperation.builder()
    .addFieldWithValueOf('name', 'John')
    .addFieldWithValueOf('age', 25)
    .build();
const doc = operation.toDocument(context);

// => { $addFields: { name: "$John", age: 25 } }
const operation = AddFieldsOperation.builder()
 .addField('fullName')
 .withValueOf(ArithmeticOperators.add('$firstName').add('$lastName'))
 .addField('discount')
 .withValueOf(ArithmeticOperators.multiply('$price').multiplyBy(0.1))
 .build();

const doc = operation.toDocument(context);

// => {
//      $addFields: {
//          fullName: { $add: ["$firstName", "$lastName"] },
//          discount: { $multiply: ["$price", 0.1] }
//      }
//    }