admin管理员组文章数量:1333211
I have a document fetched as:
Document
.find(<condition>)
.exec()
.then(function (fetchedDocument) {
console.log(fetchedDocument);
});
Now this document has a reference to another document. But I am not populating that reference when I'm querying this document. Instead, I wanna populate it later. So is there any way of doing that? Can I do this:
fetchedDocument
.populate('field')
.exec()
.then(function (reFetchedDocument) {
console.log(reFetchedDocument);
});
Another method I came across is to do this:
Document
.find(fetchedDocument)
.populate('field')
.then(function (reFetchedDocument) {
console.log(reFetchedDocument);
});
Now does this re-fetch the entire document all over again or just fetch the populated part and add it in?
I have a document fetched as:
Document
.find(<condition>)
.exec()
.then(function (fetchedDocument) {
console.log(fetchedDocument);
});
Now this document has a reference to another document. But I am not populating that reference when I'm querying this document. Instead, I wanna populate it later. So is there any way of doing that? Can I do this:
fetchedDocument
.populate('field')
.exec()
.then(function (reFetchedDocument) {
console.log(reFetchedDocument);
});
Another method I came across is to do this:
Document
.find(fetchedDocument)
.populate('field')
.then(function (reFetchedDocument) {
console.log(reFetchedDocument);
});
Now does this re-fetch the entire document all over again or just fetch the populated part and add it in?
Share Improve this question asked Apr 3, 2015 at 10:35 BadgerBadgerBadgerBadgerBadgerBadgerBadgerBadger 3744 silver badges19 bronze badges1 Answer
Reset to default 9Your second example (with Document.find(fetchedDocument)
) is highly inefficient. It's not only re-fetching the whole document from MongoDB, it's also uses all fields of previously fetched document to match against MongoDB collection (not just _id
field). So, if some part of your document changed between two requests, this code won't find your document.
Your first example (with fetchedDocument.populate
) is fine, except for .exec()
part.
Document#populate
method returns a Document
, not a Query
, so there is not .exec()
method. You should use special .execPopulate()
method instead:
fetchedDocument
.populate('field')
.execPopulate()
.then(function (reFetchedDocument) {
console.log(reFetchedDocument);
});
本文标签: javascriptPopulating on an already fetched document Is it possible and if sohowStack Overflow
版权声明:本文标题:javascript - Populating on an already fetched document. Is it possible and if so, how? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742276562a2445305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论