繁体   English   中英

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

[英]Populate collection inside another collection mongoDB with mongoose nodejs

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

我在猫鼬中有以下模板:

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"
    }
  ], ....
}

但我想要更多关于所有者用户的信息(姓名和照片路径)。

如何填充这些附加字段?

这就是我要找的:

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

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

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 文档中查看深度人口

尝试这个...

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM