MongoDB查询,通过用户ID查找全部 [英] MongoDB Query, find all by userID
本文介绍了MongoDB查询,通过用户ID查找全部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的数据库中客户"的结构
Here is the structure of "customers" in my db
{
"_id": {
"$oid": "xxxxx"
},
"user": {
"$oid": "xxxxx"
},
"name": "Test Mobile",
"email": null,
"phone": "xxxxx",
"completed": false,
"__v": 0
}
我正在尝试使用某个用户ID查询所有客户,并按完成"排序,这样我就可以获得所有客户.
I am trying to query all customers with a certain userID and sort by "completed", I can get all customers like so
exports.list = function(req, res, next) {
Customer.find().sort('-completed').exec(function(err, customers) {
if (err) return next(err);
return res.send(customers);
})
};
我可以像这样基于电话查询
I can query based on phone like so
exports.list = function(req, res, next) {
Customer.find({ phone: "xxxxxxxx"}).sort('-completed').exec(function(err, customers) {
if (err) return next(err);
return res.send(customers);
})
};
我似乎无法做到的是查询用户ID,我尝试过这种方式,但这种方式很不错,但是没有运气
what I can't seem to do is query the userid, i tried this way and variations of this but no luck
exports.list = function(req, res, next) {
Customer.find({"user.$oid": ObjectId("xxxxxxxxx")}).sort('-completed').exec(function(err, customers) {
if (err) return next(err);
return res.send(customers);
})
};
不确定在查询ID时我是否缺少简单的东西?
Not sure if i'm missing something simple when querying ID's?
推荐答案
尝试一下:
.find({user: xxxx)})
没有ObjectId
Without ObjectId
这篇关于MongoDB查询,通过用户ID查找全部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文