Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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 Mongoose查询比较对象ID_Node.js_Mongodb_Mongoose - Fatal编程技术网

Node.js Mongoose查询比较对象ID

Node.js Mongoose查询比较对象ID,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我正试图从我的数据库中获取一些文档。在每个文档中,都有一个名为“所有者”的字段,它是用户的ObjectId。我想获取特定用户的所有文档。我有用户id,当我尝试这样做时: exports.getBoxes = function(req, res) { const { user } = res.locals; const query = db.Box.find(); query.where('owner').equals(user._id); query.exec(f

我正试图从我的数据库中获取一些文档。在每个文档中,都有一个名为“所有者”的字段,它是用户的ObjectId。我想获取特定用户的所有文档。我有用户id,当我尝试这样做时:

exports.getBoxes = function(req, res) {
    const { user } = res.locals;
    const query = db.Box.find();
    query.where('owner').equals(user._id);
    query.exec(function(err, boxes) {
        console.log(boxes);
    });
}
我得到一个空数组。我在我的数据库中看到了许多对应于此查询的框。怎么了

更新 这是我的模式:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const timestamps = require('mongoose-timestamps');

const BoxSchema = new Schema({
  description: {
    type: String,
    trim: true
  },
  producer: {
    type: String,
    trim: true
  },
  cycle: {
    type: String,
    trim: true
  },
  owner: {
    type: Schema.ObjectId,
    ref: 'Supplier'
  },
  event: {
    type: Schema.ObjectId,
    ref: 'Event'
  },
  type: {
    type: String,
    enum: []
  },
  creditTerms: {
    type: String,
    enum: ['Cash', '30 Days', '60 Days', '90 Days', '120 Days']
  },
  bids: [{
    type: Schema.ObjectId,
    ref: 'Bid'
  }],
  looking: [{
    type: Schema.ObjectId,
    ref: 'User'
  }],
  sold: Boolean,
  paid: Boolean,
  delivered: Boolean,
  sealed: Boolean,
  initialPrice: Number,
  value: Number,
  cts: Number,
  ppc: Number,
  finalPrice: Number
});

BoxSchema.plugin(timestamps);

module.exports = mongoose.model('Box', BoxSchema);
下面是我试图获取的文档示例:

好了,伙计们,我设法解决了这个问题。问题是box架构中的owner字段引用的是供应商对象,而不是用户对象。所以我就这样解决了:

const { user } = res.locals;
return db.Supplier.findOne({ userId: user._id })
       .populate('boxes').exec(function(err, supplier) {
            if(err || !supplier) return res.sendStatus(404);

            res.json(supplier.boxes);
        });

我建议您,因为我不明白您的查询是如何将新的mongodb.ObjectID(user.id)用于_id比较到_id比较的。这个假设框中有数据,您当然会得到一个查询,而不知道它是如何构造的。这应该可以解决您的问题:-exports.getbox=function(req,res){const{user}=res.locals;const query=db.Box.find({“owner”:user._id});query.exec(function(err,Box){console.log(Box)});}@阿披实辛:它不起作用了。它仍然以空数组响应