Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The SDK database Aggregate adds records


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Aggregate.addFields(object: Object): Aggregate

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregation phase. A dd a new field to the output record. After the addFields aggregation phase, all records output will have fields specified by addFields in addition to the fields that were entered.

Parameters

object: Object

Returns a value

Aggregate

API description

AddFields is equivalent to specifying the project stage for all existing and new fields at the same time.

AddFields takes the form of:

addFields({
  <新字段>: <表达式>
})

AddFields can specify multiple new fields, each with a value determined by the expression used.

If you specify a new field to rename the original field, the value of the new field overrides the value of the original field. Note addFields cannot be used to add elements to array fields.

Example 1: AddFields twice in a row

Suppose the collection scores have the following records:

{
  _id: 1,
  student: "Maya",
  homework: [ 10, 5, 10 ],
  quiz: [ 10, 8 ],
  extraCredit: 0
}
{
  _id: 2,
  student: "Ryan",
  homework: [ 5, 6, 5 ],
  quiz: [ 8, 8 ],
  extraCredit: 8
}

Apply addFields twice, the first adding two fields to the values of homework and quiz, and the second adding a field to the values of the last two and then one to the values.

const $ = db.command.aggregate
db.collection('scores').aggregate()
  .addFields({
    totalHomework: $.sum('$homework'),
    totalQuiz: $.sum('$quiz')
  })
  .addFields({
    totalScore: $.add(['$totalHomework', '$totalQuiz', '$extraCredit'])
  })
  .end()

The results are as follows:

{
  "_id" : 1,
  "student" : "Maya",
  "homework" : [ 10, 5, 10 ],
  "quiz" : [ 10, 8 ],
  "extraCredit" : 0,
  "totalHomework" : 25,
  "totalQuiz" : 18,
  "totalScore" : 43
}
{
  "_id" : 2,
  "student" : "Ryan",
  "homework" : [ 5, 6, 5 ],
  "quiz" : [ 8, 8 ],
  "extraCredit" : 8,
  "totalHomework" : 16,
  "totalQuiz" : 16,
  "totalScore" : 40
}

Example 2: Add fields to nested records

You can add fields to nested records using point notation. Suppose the vehicles collection contains the following records:

{ _id: 1, type: "car", specs: { doors: 4, wheels: 4 } }
{ _id: 2, type: "motorcycle", specs: { doors: 0, wheels: 2 } }
{ _id: 3, type: "jet ski" }

You can add a new field under the specs field by fuel_type, and the values are set to the fixed string unleaded:

db.collection('vehicles').aggregate()
  .addFields({
    'spec.fuel_type': 'unleaded'
  })
  .end()

The results are as follows:

{ _id: 1, type: "car",
   specs: { doors: 4, wheels: 4, fuel_type: "unleaded" } }
{ _id: 2, type: "motorcycle",
   specs: { doors: 0, wheels: 2, fuel_type: "unleaded" } }
{ _id: 3, type: "jet ski",
   specs: { fuel_type: "unleaded" } }

Example 3: Set the field value to another field

You can set the value of a field to the value of another field by using a string of field names as an expression of value.

Also with the previous collection example, you can add a field vehicle_type set its value to the type field as follows:

db.collection('vehicles').aggregate()
  .addFields({
    vehicle_type: '$type'
  })
  .end()

The results are as follows:

{ _id: 1, type: "car", vehicle_type: "car",
   specs: { doors: 4, wheels: 4, fuel_type: "unleaded" } }
{ _id: 2, type: "motorcycle", vehicle_type: "motorcycle",
   specs: { doors: 0, wheels: 2, fuel_type: "unleaded" } }
{ _id: 3, type: "jet ski", vehicle_type: "jet ski",
   specs: { fuel_type: "unleaded" } }