简体   繁体   中英

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

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

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?

Try this:

.find({user: xxxx)}) 

Without ObjectId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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