I have a collection named Shops :
{
"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
Query
My query to fetch products based on the search value is :
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})
Output
[{
"_id": null,
"productList": [
{
"productId": "1234",
"productName": "sambarpowder 500 gm",
"price": "90"
},
{
"productId": "5678",
"productName": "sambarpowder 1000 gm",
"price": "38 "
}
]
}]
Problem
The problem here is that I written limit:1 in the query but it is returning 2 products. I don't know how to limit the subdocuments in this.