node.js 猫鼬用多个参数搜索 FindOne
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18884542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoose searching FindOne with multiple arguments
提问by Rob
My first attempt at building something with Angular + express + mongodb, so I'm probably going about this completely the wrong way. Express is being used to serve up json. Angular then takes care of all the views etc.
我第一次尝试使用 Angular + express + mongodb 构建一些东西,所以我可能完全以错误的方式来解决这个问题。Express 用于提供 json。然后 Angular 会处理所有的视图等。
I'm using Mongoose to interact with Mongo.
我正在使用 Mongoose 与 Mongo 进行交互。
I have the following database schema:
我有以下数据库架构:
var categorySchema = new mongoose.Schema({
title: String, // this is the Category title
retailers : [
{
title: String, // this is the retailer title
data: { // this is the retailers Data
strapLine: String,
img: String , // this is the retailer's image
intro: String,
website: String,
address: String,
tel: String,
email: String
}
}
]
});
var Category = mongoose.model('Category', categorySchema);
and in Express I have a couple of routes to get the data:
在 Express 中,我有几条路线来获取数据:
app.get('/data/categories', function(req, res) {
// Find all Categories.
Category.find(function(err, data) {
if (err) return console.error(err);
res.json(data)
});
});
// return a list of retailers belonging to the category
app.get('/data/retailer_list/:category', function(req, res) {
//pass in the category param (the unique ID), and use that to do our retailer lookup
Category.findOne({ _id: req.params.category }, function(err, data) {
if (err) return console.error(err);
res.json(data)
});
});
The above works - I'm just having big problems trying to get at a single retailer. I'm passing the category, and retailer id through... I've tried all sorts of things - from doing a find on the category, then a findOne on the contents within... but I just cant get it to work. I'm probably going about this all wrong...
以上工作 - 我只是在试图进入一家零售商时遇到了大问题。我正在传递类别和零售商 id... 我已经尝试了各种各样的事情 - 从对类别进行查找,然后对其中的内容进行查找......但我无法让它工作。我可能在这一切都错了......
I found this thread here: findOne Subdocument in Mongooseand implemented the solution - however, it returns all my retailers - and not just the one I want.
我在这里找到了这个线程:findOne Subdocument in Mongoose并实施了解决方案 - 但是,它返回了我所有的零售商 - 而不仅仅是我想要的零售商。
// Returns a single retailer
app.get('/data/retailer_detail/:category/:id', function(req, res) {
//pass in the category param (the unique ID), and use that to do our retailer lookup
Category.findOne({_id: req.params.category , 'retailers.$': 1}, function(err, data) {
console.log(data);
if (err) return console.error(err);
res.json(data)
});
});
Thanks, Rob
谢谢,罗布
回答by WiredPrairie
Now that I see your full filter/query, you should be able to use the array positional operatorin this case as part of the projection rather than doing client side filtering:
现在我看到了您的完整过滤器/查询,在这种情况下,您应该能够将数组位置运算符用作投影的一部分,而不是进行客户端过滤:
app.get('/data/retailer_detail/:category/:id', function(req, res) {
//pass in the category param (the unique ID), and use that to do our retailer lookup
Category.findOne({
/* query */
_id: req.params.category ,
'retailers._id' : req.params.id
},
{ /* projection */
"retailers.$" : 1
},
function(err, data) {
var retailer = _.where(data.retailers , { id : req.params.id });
if (err) return console.error(err);
res.json(retailer)
});
});
For the { "retailers.$" : 1 }to work properly, the query must include a field from an element in the array. The $operator returns the first match only.
为了使{ "retailers.$" : 1 }正常工作,查询必须包含来自数组元素的字段。该$运营商的回报第一只匹配。
回答by Rob
The guys next door use Mongo + Express and gave me some pointers: they explained to me how mongo worked, and advised I should use underscore.js to assist with my filter.
隔壁的人使用 Mongo + Express 并给了我一些指示:他们向我解释了 mongo 是如何工作的,并建议我应该使用 underscore.js 来协助我的过滤器。
They said I needed to pull the entire category out - and then run the filter. I don't strictly need , 'retailers._id' : req.params.id} but they said to leave it in as it guaranteed that the category would only be returned if an item within it contained that information. I still don't really know why or how... So can't really mark this as solved.. it it solved, but I don't really get why as yet - so will do more reading :)
他们说我需要拉出整个类别 - 然后运行过滤器。我并不严格需要 , 'retailers._id' : req.params.id} 但他们说把它留在里面,因为它保证只有当其中的一个项目包含该信息时才会返回该类别。我仍然不知道为什么或如何......所以不能真正将其标记为已解决......它解决了,但我还不明白为什么 - 所以会做更多的阅读:)
app.get('/data/retailer_detail/:category/:id', function(req, res) {
//pass in the category param (the unique ID), and use that to do our retailer lookup
Category.findOne({_id: req.params.category , 'retailers._id' : req.params.id}, function(err, data) {
var retailer = _.where(data.retailers , { id : req.params.id });
if (err) return console.error(err);
res.json(retailer)
});
});

