新聞中心
Aggregate.unwind(value:string|object): Aggregate
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
成都創(chuàng)新互聯(lián)是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計,網(wǎng)站模板,微信公眾號開發(fā),軟件開發(fā),小程序設(shè)計,十載建站對成都搬家公司等多個方面,擁有豐富設(shè)計經(jīng)驗。
聚合階段。使用指定的數(shù)組字段中的每個元素,對文檔進行拆分。拆分后,文檔會從一個變?yōu)橐粋€或多個,分別對應數(shù)組的每個元素。
參數(shù)
value: string|object
返回值
Aggregate
API 說明
使用指定的數(shù)組字段中的每個元素,對文檔進行拆分。拆分后,文檔會從一個變?yōu)橐粋€或多個,分別對應數(shù)組的每個元素。
unwind 有兩種使用形式:
- 參數(shù)是一個字段名
unwind(<字段名>)
- 參數(shù)是一個對象
unwind({
path: <字段名>,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
})
| 字段 | 類型 | 說明 |
|---|---|---|
| path | string | 想要拆分的數(shù)組的字段名,需要以 $ 開頭。 |
| includeArrayIndex | string | 可選項,傳入一個新的字段名,數(shù)組索引會保存在這個新的字段上。新的字段名不能以 $ 開頭。 |
| preserveNullAndEmptyArrays | boolean | 如果為 true,那么在 path 對應的字段為 null、空數(shù)組或者這個字段不存在時,依然會輸出這個文檔;如果為 false,unwind 將不會輸出這些文檔。默認為 false。 |
示例
拆分數(shù)組
假設(shè)我們有一個 products 集合,包含數(shù)據(jù)如下:
{ "_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"] }
我們根據(jù) size 字段對這些文檔進行拆分
db.collection('products')
.aggregate()
.unwind('$size')
.end()
輸出如下:
{ "_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" }
拆分后,保留原數(shù)組的索引
我們根據(jù) size 字段對文檔進行拆分后,想要保留原數(shù)組索引在新的 index 字段中。
db.collection('products')
.aggregate()
.unwind({
path: '$size',
includeArrayIndex: 'index'
})
.end()
輸出如下:
{ "_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 }
保留字段為空的文檔
注意到我們的集合中有兩行特殊的空值數(shù)據(jù):
...
{ "_id": "2", "product": "pants", "size": [] }
{ "_id": "3", "product": "socks", "size": null }
...
如果想要在輸出中保留 size 為空數(shù)組、null,或者 size 字段不存在的文檔,可以使用 preserveNullAndEmptyArrays 參數(shù)
db.collection('products')
.aggregate()
.unwind({
path: '$size',
preserveNullAndEmptyArrays: true
})
.end()
輸出如下:
{ "_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" } 本文題目:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫Aggregate·文檔拆分
URL地址:http://m.fisionsoft.com.cn/article/cciispg.html


咨詢
建站咨詢

