https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/aggregate/AggregateCommand.in.html
我想实现如下效果
假设集合 shops
有如下记录:
{ "_id": 1, "topsellers": [{ id: 1, name: 'bread' }, { id:2, name: 'ice cream' }, { id: 3, name: 'butter' }] }
{ "_id": 2, "topsellers": [{ id: 2, name: 'ice cream' }, { id: 4, name: 'cheese' }, { id: 5, name: 'yagurt' }] }
{ "_id": 3, "topsellers": [{ id: 6, name: 'croissant' }, { id: 7, name: 'cucumber' }, { id: 8, name: 'coconut' }] }
标记销量最高的商品包含 ice cream
的记录。
const $ = db.command.aggregate
db.collection('price').aggregate()
.project({
included: $.in(['ice cream', '$topsellers'])
})
.end()
返回结果如下:
{ "_id": 1, "included": true }
{ "_id": 2, "included": true }
{ "_id": 3, "included": false }
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/Command.elemMatch.html
使用elemMatch