Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Mongodb 此基本设置忽略Mongoose填充_Mongodb_Mongoose_Mongoose Schema - Fatal编程技术网

Mongodb 此基本设置忽略Mongoose填充

Mongodb 此基本设置忽略Mongoose填充,mongodb,mongoose,mongoose-schema,Mongodb,Mongoose,Mongoose Schema,我有一个用户模式,它有多个注释,注释属于一个用户ID const UserSchema = new Schema({ _id: Schema.Types.ObjectId, email: {type: String, required: true, trim: true, lowercase: true, unique: true}, notes: [{type: Schema.Types.ObjectId, ref: 'Note'}] }); const NoteSc

我有一个用户模式,它有多个注释,注释属于一个用户ID

const UserSchema = new Schema({
    _id: Schema.Types.ObjectId,
    email: {type: String, required: true, trim: true, lowercase: true, unique: true},
    notes: [{type: Schema.Types.ObjectId, ref: 'Note'}]
});

const NoteSchema = new Schema({
    userId: {type: mongoose.Types.ObjectId, ref: 'User'},
    content: {type: String, required: true, trim: true, lowercase: true},
});
我正在尝试使用以下语法(来自文档)向我的用户填充注释


但它返回的用户没有Notes数据。知道我可能做错了什么吗?

NoteSchema
问题是:

userId:{type:mongoose.Types.ObjectId,ref:'User'}

在下面使用

userId: {type: mongoose.Schema.Types.ObjectId, ref: 'User'}
// OR
userId: {type: Schema.Types.ObjectId, ref: 'User'}
// OR
userId: {type: Schema.ObjectId, ref: 'User'} // For backword compatibility
注意:-模式应始终使用
mongoose.schema.Types
。和
mongoose.Types.ObjectId
可以与mongoose实现一起使用

我能够正确获取文档(以下代码)):

输出:

[
  {
    "_id": "5bd2c84dd79cc5d8b1c62964",
    "email": "hardik@com.com",
    "notes": [
      {
        "_id": "5bd2c869d79cc5d8b1c62965",
        "content": "ABC",
        "userId": "5bd2c84dd79cc5d8b1c62964"
      },
      {
        "_id": "5bd2c88ad79cc5d8b1c62966",
        "content": "DEF",
        "userId": "5bd2c84dd79cc5d8b1c62964"
      }
    ]
  }
]

你能分享你拥有的样本文档吗?我在模式中没有发现任何问题,请确保笔记中有相关数据。
var mongoose = require('mongoose'),
Schema = mongoose.Schema;


const NoteSchema = new Schema({
    userId: {type: Schema.Types.ObjectId, ref: 'UserTest'},
    content: {type: String, required: true, trim: true, lowercase: true},
});

const UserSchema = new Schema({
  _id: Schema.Types.ObjectId,
  email: {type: String, required: true, trim: true, lowercase: true, unique: true},
  notes: [{type: Schema.Types.ObjectId, ref: 'NoteTest'}]
});

var Note = mongoose.model('NoteTest', NoteSchema);
var User = mongoose.model('UserTest', UserSchema);


User.find({_id : mongoose.Types.ObjectId("5bd2c84dd79cc5d8b1c62964")})
  .populate('notes')
  .exec(function (err, result) {
      console.log("result.....", JSON.stringify(result));
  });
[
  {
    "_id": "5bd2c84dd79cc5d8b1c62964",
    "email": "hardik@com.com",
    "notes": [
      {
        "_id": "5bd2c869d79cc5d8b1c62965",
        "content": "ABC",
        "userId": "5bd2c84dd79cc5d8b1c62964"
      },
      {
        "_id": "5bd2c88ad79cc5d8b1c62966",
        "content": "DEF",
        "userId": "5bd2c84dd79cc5d8b1c62964"
      }
    ]
  }
]