【发布时间】:2020-06-26 00:18:55
【问题描述】:
querying certain fields only 在 mongodb 查询中是否节省查询时间?
假设我有一个如下所示的架构。
const CommentSchema = new mongoose.Schema({
commentText:{
type:String,
required: true
},
arrayOfReplies: [{
replyText:{
type:String,
required: true
},
likes: [{
objectIdOfUser: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
}],
}],
});
我试图找出 mongodb 是否查询整个文档然后过滤它,或者过滤是否发生在 mongodb 内部。
如果我有一个大的arrayOfRepliesarray,例如 500,000 个回复。与查询整个文档相比,下面的查询as shown the mongoose docs 会节省查询时间吗?
Comment.findById(id, 'commentText', function (err, comment) {});
编辑
我之前曾问过这个问题(见下文)。我已经改变了我的问题,以反映我真正想问的问题。虽然下面查询的问题的答案仍然存在。
Comment.findById(id, 'replyText', function (err, comment) {});
【问题讨论】:
标签: mongodb mongoose mongodb-query