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

问题描述 投票:1回答:2

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

我在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(name和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"
  }
}
node.js mongodb mongoose mongoose-populate
2个回答
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;
}

看一下来自Mongoose docs的deep population


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;
}
© www.soinside.com 2019 - 2024. All rights reserved.