Ex。記錄MongoDB Aggregate正則表達式匹配或全文搜索返回整個文檔
[
{
"_id": "5528cfd2e71144e020cb6494",
"__v": 11,
"Product": [
{
"_id": "5528cfd2e71144e020cb6495",
"isFav": true,
"quantity": 27,
"price": 148,
"description": "100g",
"brand": "JaldiLa",
"name": "Grapes",
"sku": "GRP"
},
{
"_id": "552963ed63d867b81e18d357",
"isFav": false,
"quantity": 13,
"price": 290,
"description": "100g",
"brand": "JaldiLa",
"name": "Apple",
"sku": "APL"
}
],
"brands": [
"Whole Foods",
"Costco",
"Bee's",
"Masons"
],
"sku": "FRT",
"name": "Fruits"
}
]
我的貓鼬函數從AngularJS返回查詢(http://localhost:8080/api/search?s=)
router.route('/search')
.get(function(req, res) {
Dept.aggregate(
{ $match: { $text: { $search: req.query.s } } },
{ $project : { name : 1, _id : 1, 'Product.name' : 1, 'Product._id' : 1} },
{ $unwind : "$Product" },
{ $group : {
_id : "$_id",
Category : { $addToSet : "$name"},
Product : { $push : "$Product"}
}}
)
});
結果:例如http://localhost:8080/api/search?s=Apple /葡萄/胡蘿蔔,結果全部相同。
[
{
"_id": "5528cfd2e71144e020cb6494",
"Category": ["Fruits"],
"Product": [
{
"_id": "5528cfd2e71144e020cb6495",
"name": "Grapes"
},
{
"_id": "552963ed63d867b81e18d357",
"name": "Apple"
},
{
"_id": "552e61920c530fb848c61510",
"name": "Carrots"
}
]
}
]
問題:在「蘋果」的查詢,它返回的產品,而不是僅僅「葡萄」中的所有對象,我想也許把比賽的展開後會做的伎倆或$正則表達式的情況下
什麼我想:例如對於「葡萄」searchString 另外我希望它開始發送結果,只要我發送我的查詢的前兩個字母。
[{
"_id": ["5528cfd2e71144e020cb6494"], //I want this in array as it messes my loop up
"Category": "Fruits", //Yes I do not want this in array like I'm getting in my resutls
"Product": [{
"_id": "5528cfd2e71144e020cb6495",
"name": "Grapes"
}]
}]
感謝您的耐心。
UPDATE:{$比賽:{ '$ idol.name':新的RegExp(QUER y,'gi')}}通過鍵查詢給出一個鍵,現在剩下的東西是特定的對象只返回 –
UPDATE:{$ sort:{score:{$ meta:「textScore」},name:1}},{ $ limit:1}它返回整個文檔 –
請創建一個[Minimal,Complete,Verifiable Example](http://stackoverflow.com/help/mcve) –