我有一个名为商店的集合:
{
"ShopId" : 9999,
"products" : [
{
"productId" : "1234",
"productName" : "sambarpowder 500 gm",
"productCategory" : "masala",
"mrp" : "90",
},
{
"productId" : "5678",
"productName" : "sambarpowder 1000 gm",
"productCategory" : "masala",
"mrp" : "38 ",
}
],
},{....}--> There will be a list of shops
查询
我根据搜索值获取产品的查询是:
model.Shops.aggregate(
{$match:{"ShopId":9999}},{$unwind:"$products"},
{$match:{"products.productName":new RegExp('sambar','i') }},
{ $group:{_id:null,productList:{$push:{"productId":"$products.productId","productName":"$products.productName","price":"$products.mrp"}}}},{$limit:1})
输出
[{
"_id": null,
"productList": [
{
"productId": "1234",
"productName": "sambarpowder 500 gm",
"price": "90"
},
{
"productId": "5678",
"productName": "sambarpowder 1000 gm",
"price": "38 "
}
]
}]
问题
问题在于我在查询中写了 limit:1 ,但它返回 2个产品。我不知道如何限制这个子文档。