【问题标题】:Populate collection inside another collection mongoDB with mongoose nodejs使用 mongoose nodejs 在另一个集合 mongoDB 中填充集合
【发布时间】:2019-08-07 22:40:45
【问题描述】:

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

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

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

如何填充这些附加字段?

这就是我要找的:

{
  "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


    【解决方案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 文档的 deep population

    【讨论】:

    • 非常感谢您提供的所有帮助。使用此代码,我已经获得了与我想要的结果相似的结果,我将看看我是否可以达到最终目标。这是我在这里的第一篇文章。也感谢您编辑它的时间,它显然更好。
    • 乐于帮助@RuiRocha
    【解决方案2】:

    试试这个...

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

    【讨论】:

    • 我已经尝试过这个解决方案。 @Don Brody 的解决方案违背了我的意图,但我仍然需要做出一些改变。感谢您的光临。
    猜你喜欢
    • 2019-01-14
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2013-11-18
    • 2020-07-11
    • 2023-01-10
    • 1970-01-01
    • 2013-07-01
    相关资源
    最近更新 更多