数据库中数据结构:
{
"_id": {
"$oid": "607f8fb2f1cf542ae93e0fd6"
},
"cal_id": {
"$numberLong": "2"
},
"attach": {
"goods": {
"list": [{
"id": {
"$numberLong": "257"
},
"name": "商品1",
"desc": "desc",
"img_url": "https://www.baidu.com",
"price": "$200",
"buy_url": "https://www.baidu.com"
}, {
"id": {
"$numberLong": "258"
},
"name": "商品2",
"desc": "desc",
"img_url": "https://www.baidu.com",
"price": "$20",
"buy_url": "https://www.baidu.com"
}]
}
}
}
(返回数组中元素的数量)查询list中包含的商品数量sql语句:
(mongo Shell 中执行)
db.config.aggregate([{$match:{"cal_id":2}},{$project:{count:{ $size: "$attach.goods.list" }}}])
//拼接聚合条件:1.筛选符合日程的document2.返回商品数组中商品数量