简体   繁体   English

MongoDB查询,通过用户ID查找全部

[英]MongoDB Query, find all by userID

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
}

I am trying to query all customers with a certain userID and sort by "completed", I can get all customers like so 我正在尝试使用某个用户ID查询所有客户,并按“完成”排序,这样我就可以获得所有客户

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);
  })
};

what I can't seem to do is query the userid, i tried this way and variations of this but no luck 我似乎无法做的是查询userid,我尝试了这种方式以及这种方式的变化,但没有运气

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);
  })
};

Not sure if i'm missing something simple when querying ID's? 不知道查询ID时是否缺少简单的东西?

Try this: 尝试这个:

.find({user: xxxx)}) 

Without ObjectId 没有ObjectId

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM