使用 mongoose nodejs 在另一个集合 mongoDB 中填充集合
Populate collection inside another collection mongoDB with mongoose nodejs
我正在尝试从特定用户处获取所有信息。我已经可以填充它包含的集合,但我无法在这些集合中填充一个非常重要的属性,即用户。我有以下代码重印模型和我已经为查询完成的代码。我还展示了我可以获得的结果和我想要的示例。谢谢你帮助我。
我在 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"
}
}
您并没有要求在您的代码中填充这些字段。如果你想从一个对象返回所有的字段,试试这样的事情:
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。
试试这个...
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;
}
我正在尝试从特定用户处获取所有信息。我已经可以填充它包含的集合,但我无法在这些集合中填充一个非常重要的属性,即用户。我有以下代码重印模型和我已经为查询完成的代码。我还展示了我可以获得的结果和我想要的示例。谢谢你帮助我。
我在 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"
}
}
您并没有要求在您的代码中填充这些字段。如果你想从一个对象返回所有的字段,试试这样的事情:
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。
试试这个...
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;
}