Javascript 在猫鼬中使用populate

Javascript 在猫鼬中使用populate,javascript,mongoose,populate,Javascript,Mongoose,Populate,我是mongoose的新手,我尝试让填充工作,但返回值总是空的,好像引用在集合中不存在,这是错误的:-) 这是我的密码: mongoose.connect('mongodb://192.168.1.119:27017/DIM').then(() => { let p = mongoose.model( 'Profile', new mongoose.Schema({ _id: mongoose.Schema.Types.ObjectId, Nam

我是mongoose的新手,我尝试让填充工作,但返回值总是空的,好像引用在集合中不存在,这是错误的:-)

这是我的密码:

mongoose.connect('mongodb://192.168.1.119:27017/DIM').then(() => {
  let p = mongoose.model(
    'Profile',
    new mongoose.Schema({
      _id: mongoose.Schema.Types.ObjectId,
      Name: mongoose.Schema.Types.String,
    }),
    'Profile'
  )

  let m = mongoose.model(
    'User',
    new mongoose.Schema({
      _id: mongoose.Schema.Types.ObjectId,
      Email: mongoose.Schema.Types.String,
      ProfileId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Profile',
      },
    }),
    'User'
  )

  try {
    m.find()
      .populate('ProfileId')
      .select('ProfileId Email _id')
      .exec()
      .then(ret => {
        console.log(JSON.stringify(ret))
      })
  } catch (err) {
    console.log('Error ' + err.message)
  }
})
这是我得到的回报:

[{“\u id”:“5b3ca85a2fcf013a04594f79”,“电子邮件”:“sa”,“ProfileId”:null},{“\u id”:“5b3ca85a2fcf013a04594f7d”,“电子邮件”:“远程日志”,“ProfileId”:null}]

profileId始终为空

下面是数据库的内容

我一定是在做傻事,但似乎找不到地方


谢谢

顾名思义是指“数据库中不存在的数据值”。您的收藏中是否存储了要读取的内容?

好的,问题是我连接的数据库是由c应用程序创建的,其中的#id不是Mongo ObjectId而是字符串GUID,因此我的代码必须适应:

    let p = mongoose.model(
    'Profile',
    new mongoose.Schema({
      _id: mongoose.Schema.Types.String,
      Name: mongoose.Schema.Types.String,
    }),
    'Profile'
  )

  let m = mongoose.model(
    'User',
    new mongoose.Schema({
      _id: mongoose.Schema.Types.String,
      Email: mongoose.Schema.Types.String,
      ProfileId: {
        type: mongoose.Schema.Types.String,
        ref: 'Profile',
      },
    }),
    'User'
  )

是的,的确如此,但是从我添加的截图中你可以看到这个系列中有一些东西。所以我认为它不应该是空的。