新聞中心
AggregateCommand.allElementsTrue(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè)|成都網(wǎng)站維護(hù)|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋成都護(hù)欄打樁機(jī)等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身建設(shè)品質(zhì)網(wǎng)站。
聚合操作符。輸入一個(gè)數(shù)組,或者數(shù)組字段的表達(dá)式。如果數(shù)組中所有元素均為真值,那么返回 true,否則返回 false??諗?shù)組永遠(yuǎn)返回 true。
參數(shù)
value: Expression[]
[
返回值
Object
API 說(shuō)明
語(yǔ)法如下:
allElementsTrue([])
示例代碼
假設(shè)集合 test 有如下記錄:
{ "_id": 1, "array": [ true ] }
{ "_id": 2, "array": [ ] }
{ "_id": 3, "array": [ false ] }
{ "_id": 4, "array": [ true, false ] }
{ "_id": 5, "array": [ 0 ] }
{ "_id": 6, "array": [ "stark" ] }
下面的代碼使用 allElementsTrue(),判斷 array 字段中是否均為真值:
const $ = db.command.aggregate
db.collection('price')
.aggregate()
.project({
isAllTrue: $.allElementsTrue(['$array'])
})
.end()
返回結(jié)果如下:
{ "_id": 1, "isAllTrue": true }
{ "_id": 2, "isAllTrue": true }
{ "_id": 3, "isAllTrue": false }
{ "_id": 4, "isAllTrue": false }
{ "_id": 5, "isAllTrue": false }
{ "_id": 6, "isAllTrue": true }
AggregateCommand.anyElementTrue(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。輸入一個(gè)數(shù)組,或者數(shù)組字段的表達(dá)式。如果數(shù)組中任意一個(gè)元素為真值,那么返回 true,否則返回 false??諗?shù)組永遠(yuǎn)返回 false。
參數(shù)
value: Expression[]
[
返回值
Object
API 說(shuō)明
語(yǔ)法如下:
anyElementTrue([])
示例代碼
假設(shè)集合 test 有如下記錄:
{ "_id": 1, "array": [ true ] }
{ "_id": 2, "array": [ ] }
{ "_id": 3, "array": [ false ] }
{ "_id": 4, "array": [ true, false ] }
{ "_id": 5, "array": [ 0 ] }
{ "_id": 6, "array": [ "stark" ] }
下面的代碼使用 anyElementTrue(),判斷 array 字段中是否含有真值:
const $ = db.command.aggregate
db.collection('price')
.aggregate()
.project({
isAnyTrue: $.anyElementTrue(['$array'])
})
.end()
返回結(jié)果如下:
{ "_id": 1, "isAnyTrue": true }
{ "_id": 2, "isAnyTrue": false }
{ "_id": 3, "isAnyTrue": false }
{ "_id": 4, "isAnyTrue": true }
{ "_id": 5, "isAnyTrue": false }
{ "_id": 6, "isAnyTrue": true }
AggregateCommand.setDifference(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符,輸入兩個(gè)集合,輸出只存在于第一個(gè)集合中的元素。
參數(shù)
value: Expression[]
[
返回值
Object
API 說(shuō)明
使用形式如下:
setDifference([, ])
示例代碼
假設(shè)集合 test 存在以下數(shù)據(jù):
{ "_id": 1, "A": [ 1, 2 ], "B": [ 1, 2 ] }
{ "_id": 2, "A": [ 1, 2 ], "B": [ 2, 1, 2 ] }
{ "_id": 3, "A": [ 1, 2 ], "B": [ 1, 2, 3 ] }
{ "_id": 4, "A": [ 1, 2 ], "B": [ 3, 1 ] }
{ "_id": 5, "A": [ 1, 2 ], "B": [ ] }
{ "_id": 6, "A": [ 1, 2 ], "B": [ {}, [] ] }
{ "_id": 7, "A": [ ], "B": [ ] }
{ "_id": 8, "A": [ ], "B": [ 1 ] }
下面的代碼使用 setDifference,找到只存在于 B 中的數(shù)字:
db.collection('test')
.aggregate()
.project({
isBOnly: $.setDifference(['$B', '$A'])
})
.end()
{ "_id": 1, "isBOnly": [] }
{ "_id": 2, "isBOnly": [3] }
{ "_id": 3, "isBOnly": [3] }
{ "_id": 4, "isBOnly": [5] }
{ "_id": 5, "isBOnly": [] }
{ "_id": 6, "isBOnly": [{}, []] }
{ "_id": 7, "isBOnly": [] }
{ "_id": 8, "isBOnly": [1] }
AggregateCommand.setEquals(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符,輸入兩個(gè)集合,判斷兩個(gè)集合中包含的元素是否相同(不考慮順序、去重)。
參數(shù)
value: Expression[]
[
返回值
Object
API 說(shuō)明
使用形式如下:
setEquals([, ])
示例代碼
假設(shè)集合 test 存在以下數(shù)據(jù):
{ "_id": 1, "A": [ 1, 2 ], "B": [ 1, 2 ] }
{ "_id": 2, "A": [ 1, 2 ], "B": [ 2, 1, 2 ] }
{ "_id": 3, "A": [ 1, 2 ], "B": [ 1, 2, 3 ] }
{ "_id": 4, "A": [ 1, 2 ], "B": [ 3, 1 ] }
{ "_id": 5, "A": [ 1, 2 ], "B": [ ] }
{ "_id": 6, "A": [ 1, 2 ], "B": [ {}, [] ] }
{ "_id": 7, "A": [ ], "B": [ ] }
{ "_id": 8, "A": [ ], "B": [ 1 ] }
下面的代碼使用 setEquals,判斷兩個(gè)集合中包含的元素是否相同:
db.collection('test')
.aggregate()
.project({
sameElements: $.setEquals(['$A', '$B'])
})
.end()
{ "_id": 1, "sameElements": true }
{ "_id": 2, "sameElements": true }
{ "_id": 3, "sameElements": false }
{ "_id": 4, "sameElements": false }
{ "_id": 5, "sameElements": false }
{ "_id": 6, "sameElements": false }
{ "_id": 7, "sameElements": true }
{ "_id": 8, "sameElements": false }
AggregateCommand.setIntersection(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符,輸入兩個(gè)集合,輸出兩個(gè)集合的交集。
參數(shù)
value: Expression[]
[
返回值
Object
API 說(shuō)明
使用形式如下:
setIntersection([, ])
示例代碼
假設(shè)集合 test 存在以下數(shù)據(jù):
{ "_id": 1, "A": [ 1, 2 ], "B": [ 1, 2 ] }
{ "_id": 2, "A": [ 1, 2 ], "B": [ 2, 1, 2 ] }
{ "_id": 3, "A": [ 1, 2 ], "B": [ 1, 2, 3 ] }
{ "_id": 4, "A": [ 1, 2 ], "B": [ 3, 1 ] }
{ "_id": 5, "A": [ 1, 2 ], "B": [ ] }
{ "_id": 6, "A": [ 1, 2 ], "B": [ {}, [] ] }
{ "_id": 7, "A": [ ], "B": [ ] }
{ "_id": 8, "A": [ ], "B": [ 1 ] }
下面的代碼使用 setIntersection,輸出兩個(gè)集合的交集:
db.collection('test')
.aggregate()
.project({
commonToBoth: $.setIntersection(['$A', '$B'])
})
.end()
輸出如下:
{ "_id": 1, "commonToBoth": [ 1, 2 ] }
{ "_id": 2, "commonToBoth": [ 1, 2 ] }
{ "_id": 3, "commonToBoth": [ 1, 2 ] }
{ "_id": 4, "commonToBoth": [ 1 ] }
{ "_id": 5, "commonToBoth": [ ] }
{ "_id": 6, "commonToBoth": [ ] }
{ "_id": 7, "commonToBoth": [ ] }
{ "_id": 8, "commonToBoth": [ ] }
AggregateCommand.setIsSubset(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符,輸入兩個(gè)集合,判斷第一個(gè)集合是否是第二個(gè)集合的子集。
參數(shù)
value: Expression[]
[
返回值
Object
API 說(shuō)明
使用形式如下:
setIsSubset([, ])
示例代碼
假設(shè)集合 test 存在以下數(shù)據(jù):
{ "_id": 1, "A": [ 1, 2 ], "B": [ 1, 2 ] }
{ "_id": 2, "A": [ 1, 2 ], "B": [ 2, 1, 2 ] }
{ "_id": 3, "A": [ 1, 2 ], "B": [ 1, 2, 3 ] }
{ "_id": 4, "A": [ 1, 2 ], "B": [ 3, 1 ] }
{ "_id": 5, "A": [ 1, 2 ], "B": [ ] }
{ "_id": 6, "A": [ 1, 2 ], "B": [ {}, [] ] }
{ "_id": 7, "A": [ ], "B": [ ] }
{ "_id": 8, "A": [ ], "B": [ 1 ] }
下面的代碼使用 setIsSubset,判斷第一個(gè)集合是否是第二個(gè)集合的子集:
db.collection('test')
.aggregate()
.project({
AisSubsetOfB: $.setIsSubset(['$A', '$B'])
})
.end()
{ "_id": 1, "AisSubsetOfB": true }
{ "_id": 2, "AisSubsetOfB": true }
{ "_id": 3, "AisSubsetOfB": true }
{ "_id": 4, "AisSubsetOfB": false }
{ "_id": 5, "AisSubsetOfB": false }
{ "_id": 6, "AisSubsetOfB": false }
{ "_id": 7, "AisSubsetOfB": true }
{ "_id": 8, "AisSubsetOfB": true }
AggregateCommand.setUnion(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符,輸入兩個(gè)集合,輸出兩個(gè)集合的并集。
參數(shù)
value: Expression[]
[
返回值
Object
API 說(shuō)明
使用形式如下:
setUnion([, ])
示例代碼
假設(shè)集合 test 存在以下數(shù)據(jù):
{ "_id": 1, "A": [ 1, 2 ], "B": [ 1, 2 ] }
{ "_id": 2, "A": [ 1, 2 ], "B": [ 2, 1, 2 ] }
{ "_id": 3, "A": [ 1, 2 ], "B": [ 1, 2, 3 ] }
{ "_id": 4, "A": [ 1, 2 ], "B": [ 3, 1 ] }
{ "_id": 5, "A": [ 1, 2 ], "B": [ ] }
{ "_id": 6, "A": [ 1, 2 ], "B": [ {}, [] ] }
{ "_id": 7, "A": [ ], "B": [ ] }
{ "_id": 8, "A": [ ], "B": [ 1 ] }
下面的代碼使用 setUnion,輸出兩個(gè)集合的并集:
db.collection('test')
.aggregate()
.project({
AB: $.setUnion(['$A', '$B'])
})
.end()
輸出如下:
{ "_id": 1, "AB": [ 1, 2 ] }
{ "_id": 2, "AB": [ 1, 2 ] }
{ "_id": 3, "AB": [ 1, 2, 3 ] }
{ "_id": 4, "AB": [ 1, 2, 3 ] }
{ "_id": 5, "AB": [ 1, 2 ] }
{ "_id": 6, "AB": [ 1, 2, {}, [] ] }
{ "_id": 7, "AB": [ ] }
{ "_id": 8, "AB": [ 1 ] }
分享文章:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫(kù)Command·聚合操作符·集合操作符
URL鏈接:http://m.fisionsoft.com.cn/article/dhhsdpc.html


咨詢
建站咨詢

