Mongoose nodejs : find() and populate() calls



我想知道哪种方式更有效?findOne().populate()是否在对数据库的一次调用中完成?

await collection.findOne({_id: id}).populate('x');
[do stuff]

await collection.findOne({_id: id});
[do stuff]
await collection.populate('x');

我认为您应该这样做,以便您只需要定义您的模型,您必须填充哪个字段的数据。

const nestedSchema = mongoose.Schema({
child: { type: Number, ref: 'Child', autopopulate: true }
});

https://www.npmjs.com/package/mongoose-autopopulate

最新更新