新聞中心
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è)計(jì),網(wǎng)站模板,微信公眾號(hào)開發(fā),軟件開發(fā),小程序設(shè)計(jì),十載建站對(duì)成都搬家公司等多個(gè)方面,擁有豐富設(shè)計(jì)經(jīng)驗(yàn)。
聚合階段。使用指定的數(shù)組字段中的每個(gè)元素,對(duì)文檔進(jìn)行拆分。拆分后,文檔會(huì)從一個(gè)變?yōu)橐粋€(gè)或多個(gè),分別對(duì)應(yīng)數(shù)組的每個(gè)元素。
參數(shù)
value: string|object
返回值
Aggregate
API 說明
使用指定的數(shù)組字段中的每個(gè)元素,對(duì)文檔進(jìn)行拆分。拆分后,文檔會(huì)從一個(gè)變?yōu)橐粋€(gè)或多個(gè),分別對(duì)應(yīng)數(shù)組的每個(gè)元素。
unwind 有兩種使用形式:
- 參數(shù)是一個(gè)字段名
unwind(<字段名>)
- 參數(shù)是一個(gè)對(duì)象
unwind({
path: <字段名>,
includeArrayIndex: ,
preserveNullAndEmptyArrays:
})
| 字段 | 類型 | 說明 |
|---|---|---|
| path | string | 想要拆分的數(shù)組的字段名,需要以 $ 開頭。 |
| includeArrayIndex | string | 可選項(xiàng),傳入一個(gè)新的字段名,數(shù)組索引會(huì)保存在這個(gè)新的字段上。新的字段名不能以 $ 開頭。 |
| preserveNullAndEmptyArrays | boolean | 如果為 true,那么在 path 對(duì)應(yīng)的字段為 null、空數(shù)組或者這個(gè)字段不存在時(shí),依然會(huì)輸出這個(gè)文檔;如果為 false,unwind 將不會(huì)輸出這些文檔。默認(rèn)為 false。 |
示例
拆分?jǐn)?shù)組
假設(shè)我們有一個(gè) 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 字段對(duì)這些文檔進(jìn)行拆分
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 字段對(duì)文檔進(jìn)行拆分后,想要保留原數(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·文檔拆分
文章來源:http://www.5511xx.com/article/cciispg.html


咨詢
建站咨詢

