新聞中心
Aggregate.match(object: Object): Aggregate
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
為牙克石等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及牙克石網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站制作、網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè)、牙克石網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
聚合階段。根據(jù)條件過濾文檔,并且把符合條件的文檔傳遞給下一個流水線階段。
參數(shù)
object: Object
返回值
Aggregate
API 說明
match 的形式如下:
match(<查詢條件>)
查詢條件與普通查詢一致,可以用普通查詢操作符,注意 match 階段和其他聚合階段不同,不可使用聚合操作符,只能使用查詢操作符。
// 直接使用字符串
match({
name: 'Tony Stark'
})
// 使用操作符
const _ = db.command
match({
age: _.gt(18)
})
match 內(nèi)使用查詢操作符從小程序基礎(chǔ)庫 2.8.3、云函數(shù) SDK 1.3.0 開始支持。
示例
假設(shè)集合 articles 有如下記錄:
{ "_id" : "1", "author" : "stark", "score" : 80 }
{ "_id" : "2", "author" : "stark", "score" : 85 }
{ "_id" : "3", "author" : "bob", "score" : 60 }
{ "_id" : "4", "author" : "li", "score" : 55 }
{ "_id" : "5", "author" : "jimmy", "score" : 60 }
{ "_id" : "6", "author" : "li", "score" : 94 }
{ "_id" : "7", "author" : "justan", "score" : 95 }
匹配
下面是一個直接匹配的例子:
db.collection('articles')
.aggregate()
.match({
author: 'stark'
})
.end()
這里的代碼嘗試找到所有 author 字段是 stark 的文章,那么匹配如下:
{ "_id" : "1", "author" : "stark", "score" : 80 }
{ "_id" : "2", "author" : "stark", "score" : 85 }
計數(shù)
match 過濾出文檔后,還可以與其他流水線階段配合使用。
比如下面這個例子,我們使用 group 進行搭配,計算 score 字段大于 80 的文檔數(shù)量:
const _ = db.command
const $ = _.aggregate
db.collection('articles')
.aggregate()
.match({
score: _.gt(80)
})
.group({
_id: null,
count: $.sum(1)
})
.end()
返回值如下:
{ "_id" : null, "count" : 3 } 當前名稱:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫Aggregate·過濾文檔
URL分享:http://m.fisionsoft.com.cn/article/djhjpjj.html


咨詢
建站咨詢

