mongodb queries both with AND and OR
不幸的是,这个解决方案对我无效。我想允许用户按项目id或标题搜索。我确实有一些代码,可以在包含之前检查传入的q变量是否是有效的ObjectId。
Project.find({
$and : [
{ $or : [ { title: {$regex : req.body.q} }, { _id: req.body.q } ] },
{ userID: req.session.userID }
]
}).then(project => {
console.log(project);
});
样本文件:
{ "_id" : ObjectId("5afb583e8341f161a0c950af"), "title" : "Learn Node.js", "status" : "In Progress", "createdOn" : "2014-06-05 22:39:00.000Z" }
{ "_id" : ObjectId("4fe8736dc27bc8be56947d61"), "title" : "Learn MongoDB", "status" : "In Progress", "createdOn" : "2014-06-05 22:39:00.000Z" }
我错过了什么?谢谢你的帮助!