May 20, 2021 WeChat Mini Program Development Document
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.
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:
unwind(<字段名>)
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
|
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 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 }
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" }