使用mongoose nodejs在另一个集合mongoDB中填充集合

时间:2019-03-17 11:26:02

标签: node.js mongodb mongoose mongoose-populate

我正在尝试从特定用户那里获取所有信息。我已经可以填充其中包含的集合,但是无法填充这些集合中的一个非常重要的属性,即用户。我有以下代码重印了模型以及已经为查询完成的代码。我还介绍了我能得到的结果以及我想要的例子。谢谢您的帮助。

我在Mongoose中具有以下模板:

const schema = new Schema(
{
   .....
    nameEvent:
    {
        type: String,
        required: true
    },
    owner:
    {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    invited:
    [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }],
    participants:
    [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }]
});

module.exports = mongoose.model('Meeting', schema);

并且:

const schema = new Schema(
{
    name:
    {
        type: String,
        required: true
    },
   ...
    photoPath:
    {
        type: String,
        required: true
    },
    qrcodePath:
    {
        type: String,
        required: false
    },
    receiveInvites:
    {
        type: Boolean,
        required: true
    },
    invited:
    [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Meeting'
    }],
    participating:
    [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Meeting'
    }],
    owned:
    [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Meeting'
    }]
});

module.exports = mongoose.model('User', schema);

现在,我想加载所有用户信息并填充一些我需要的字段。

当我运行以下代码时:

exports.getByid = async(id) =>
{
    const res = await User.findById(id, '_id name photoPath invited participating owned friends')
    .populate('invited', 'nameEvent owner date time numberPlayer participants')
    .populate('participating', 'nameEvent owner date time numberPlayer participants')
    .populate('owned', 'nameEvent owner date time numberPlayer participants');
    return res;
}

我得到以下JSON:

"result": {
  "invited": [],
  "participating": [
    {
      "participants": [
        "5c8a9e46ba60372df4483d2f"
      ],
      "_id": "5c8a9e5dba60372df4483d30",
      "nameEvent": "terças",
      "numberPlayer": 10,
      "date": "19/03/2019",
      "time": "20:00",
      "owner": "5c8a9e46ba60372df4483d2f"
    },
    {
      "participants": [
        "5c8a9e46ba60372df4483d2f"
      ],
      "_id": "5c8aad5eba60372df4483d34",
      "nameEvent": "quintas",
      "numberPlayer": 3,
      "date": "21/03/2019",
      "time": "15:00",
      "owner": "5c8a9e46ba60372df4483d2f"
    }
  ], ....
}

但是我想要有关所有者User(名称和photoPath)的更多信息。

如何填充这些其他字段?

这就是我要寻找的:

{
  "participants": [
    "5c8a9e46ba60372df4483d2f"
  ],
  "_id": "5c8aad5eba60372df4483d34",
  "nameEvent": "quintas",
  "numberPlayer": 3,
  "date": "21/03/2019",
  "time": "15:00",
  "owner": {
    "id":"5c8a9e46ba60372df4483d2f",
    "name":"name User",
    "photoPath": "photoPath.jpg"
  }
}

2 个答案:

答案 0 :(得分:1)

您并不是要在代码中填充这些字段。如果要从对象返回所有字段,请尝试以下操作:

exports.getByid = async (id) => {
  const res = await User.findById(id, '_id name photoPath invited participating owned friends')
    .populate({ path: 'invited' })
    .populate({ path: 'participating' })
    .populate({ 
        path: 'owned',
        populate: { path: 'owner' }
    });
  return res;
}

看看猫鼬文档中的deep population

答案 1 :(得分:0)

尝试一下...

exports.getByid = async(id) =>
{
    const res = await User.findById(id, '_id name photoPath invited participating owned friends')
    .populate('invited', 'nameEvent owner date time numberPlayer participants')
    .populate('participating', 'nameEvent owner date time numberPlayer participants')
    .populate('owned', 'nameEvent owner date time numberPlayer participants');
    .populate('owned.owner', '_id name photoPath')

    return res;
}