我如何使用猫鼬在同一个查询中进行多次填充
how can i multiple populate in same query using mongoose
我需要在同一个查询中进行多次填充。
User.find({$or:[{region: "NA"},{sector:"Some Sector"}]}, function(err, user)
{
if (err)
{
res.send(err);
}
console.log(user);
res.json(user);});
Post.model.js
const PostSchema = new Schema(
{
postTitle: {
type: String,
required: true,
},
users: [{ type: Schema.Types.ObjectId, ref: 'User' }],
},
{ timestamps: true },
);
这样查询
const posts = await Post.find()
.populate('users', '_id firstName lastName email');
我需要在同一个查询中进行多次填充。
User.find({$or:[{region: "NA"},{sector:"Some Sector"}]}, function(err, user)
{
if (err)
{
res.send(err);
}
console.log(user);
res.json(user);});
Post.model.js
const PostSchema = new Schema(
{
postTitle: {
type: String,
required: true,
},
users: [{ type: Schema.Types.ObjectId, ref: 'User' }],
},
{ timestamps: true },
);
这样查询
const posts = await Post.find()
.populate('users', '_id firstName lastName email');