Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MongoDB查询,按userID查找所有内容_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js MongoDB查询,按userID查找所有内容

Node.js MongoDB查询,按userID查找所有内容,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,下面是我的数据库中“客户”的结构 我试图用某个用户ID查询所有客户,并按“已完成”排序,我可以像这样得到所有客户 exports.list = function(req, res, next) { Customer.find().sort('-completed').exec(function(err, customers) { if (err) return next(err); return res.send(customers); }) }; 我可以根据手机这样查

下面是我的数据库中“客户”的结构

我试图用某个用户ID查询所有客户,并按“已完成”排序,我可以像这样得到所有客户

exports.list = function(req, res, next) {
  Customer.find().sort('-completed').exec(function(err, customers) {
    if (err) return next(err);
    return res.send(customers);
  })
};
我可以根据手机这样查询

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,我尝试了这种方法和其他方法,但没有成功

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时是否遗漏了一些简单的内容?

尝试以下方法:

.find({user: xxxx)}) 
如果没有ObjectId,请尝试以下操作:

.find({user: xxxx)}) 

没有ObjectId时,mongo的错误消息是什么?或者什么都没有出现?请尝试
.find({“user”:ObjectId(“xxxx”)})
当我这样做时没有错误,当我尝试jcmetz时,我会得到消息:“ObjectId未定义”}。mongo的错误消息是什么?或者什么也没有发生?请尝试
.find({“user”:ObjectId(“xxxx”)})
执行此操作时没有错误,当我尝试jcmetz时,会收到消息:“ObjectId未定义”}