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

SDK database Aggregate document split


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Aggregate.unwind(value:string|object): Aggregate

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregation phase. S plit the document using each element in the specified array field. When split, the document changes from one to one or more, corresponding to each element of the array.

Parameters

value: string|object

Returns a value

Aggregate

API description

Split the document using each element in the specified array field. When split, the document changes from one to one or more, corresponding to each element of the array.

Unwind is available in two forms:

  1. The argument is a field name
unwind(<字段名>)
  1. The argument is an object
unwind({
    path: <字段名>,
    includeArrayIndex: <string>,
    preserveNullAndEmptyArrays: <boolean>
})
Field Type Description
path string The field name of the array you want to split needs to $ $ .
includeArrayIndex string Optionally, a new field name is passed in, and the array index is saved on the new field. The new field name cannot $ $ .
preserveNullAndEmptyArrays boolean If true the path still output when the field corresponding to path is null unwind not exist; false The default is false

Example

Split the score group

Suppose we have a product collection that contains the following data:

{ "_id": "1", "product": "tshirt", "size": ["S", "M", "L"] }
{ "_id": "2", "product": "pants", "size": [] }
{ "_id": "3", "product": "socks", "size": null }
{ "_id": "4", "product": "trousers", "size": ["S"] }
{ "_id": "5", "product": "sweater", "size": ["M", "L"] }

We split these documents according to the size field

db.collection('products')
  .aggregate()
  .unwind('$size')
  .end()

The output is as follows:

{ "_id": "1", "product": "tshirt", "size": "S" }
{ "_id": "1", "product": "tshirt", "size": "M" }
{ "_id": "1", "product": "tshirt", "size": "L" }
{ "_id": "4", "product": "trousers", "size": "S" }
{ "_id": "5", "product": "sweater", "size": "M" }
{ "_id": "5", "product": "sweater", "size": "L" }

After splitting, the index of the original array is preserved

After we split the document based on the size field, we want to keep the original array index in the new index field.

db.collection('products')
  .aggregate()
  .unwind({
      path: '$size',
      includeArrayIndex: 'index'
  })
  .end()

The output is as follows:

{ "_id": "1", "product": "tshirt", "size": "S", "index": 0 }
{ "_id": "1", "product": "tshirt", "size": "M", "index": 1 }
{ "_id": "1", "product": "tshirt", "size": "L", "index": 2 }
{ "_id": "4", "product": "trousers", "size": "S", "index": 0 }
{ "_id": "5", "product": "sweater", "size": "M", "index": 0 }
{ "_id": "5", "product": "sweater", "size": "L", "index": 1 }

Keep documents with empty fields

Notice that there are two special rows of empty value data in our collection:

...
{ "_id": "2", "product": "pants", "size": [] }
{ "_id": "3", "product": "socks", "size": null }
...

If you want to keep a document with size empty in the output, null, or a size field that does not exist, you can use the preserveNull AndEmptyArrays parameter

db.collection('products')
  .aggregate()
  .unwind({
      path: '$size',
      preserveNullAndEmptyArrays: true
  })
  .end()

The output is as follows:

{ "_id": "1", "product": "tshirt", "size": "S" }
{ "_id": "1", "product": "tshirt", "size": "M" }
{ "_id": "1", "product": "tshirt", "size": "L" }
{ "_id": "2", "product": "pants", "size": null }
{ "_id": "3", "product": "socks", "size": null }
{ "_id": "4", "product": "trousers", "size": "S" }
{ "_id": "5", "product": "sweater", "size": "M" }
{ "_id": "5", "product": "sweater", "size": "L" }