【发布时间】:2015-06-24 17:30:58
【问题描述】:
例如。记录
[
{
"_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"
}
]
我的 Mongoose 函数从 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"
}
]
}
]
问题:在“apple”的查询中,它返回 Product 中的所有对象,而不仅仅是“grapes”,我认为也许在展开之后放置 match 可以解决问题或 $regex case
我想要什么:例如对于“葡萄”的搜索字符串 此外,我希望它在我发送查询的前两个字母后立即开始发送结果。
[{
"_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: { $match : { '$idol.name' : new RegExp(query, 'gi') } } 给出一个键查询,现在唯一剩下的就是特定对象只返回
-
UPDATE: { $sort: { score: { $meta: "textScore" }, name: 1 } }, { $limit: 1 } 它返回整个文档
-
我不知道是否可以解决问题,我尽力了..如果缺少任何必需的信息,请告诉我。 @JonathanMee
标签: regex angularjs node.js mongodb mongoose