-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Docs: add note about execPopulate()
to populate docs
#8671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
EDIT: Look at the next comment.
const mongoose = require('mongoose');
const { Schema } = mongoose;
const assert = require('assert');
mongoose.connect('mongodb://localhost:27017/8671', { useNewUrlParser: true, useUnifiedTopology: true });
const commentSchema = new Schema({ content: String });
const Comment = mongoose.model('Comment', commentSchema);
const postSchema = new Schema({ commentsIds: [{ type: Schema.ObjectId, ref: 'Comment' }] });
const Post = mongoose.model('Post', postSchema);
async function run () {
await Promise.all([
Post.deleteMany(),
Comment.deleteMany()
]);
const [comment1, comment2] = await Comment.create([{ content: 'first comment' }, { content: 'second comment' }]);
const post = await Post.create({ commentsIds: [comment1._id, comment2._id] });
await post.populate({ path: 'commentsIds' });
assert.equal(post.commentsIds[0].content, 'first comment');
}
run().catch(console.error); The reason this is happening is because If we were to use I'll be looking into it. |
Oh, this seems to be by design. For that, we'll need to use await post.populate({ path: 'commentsIds' }).execPopulate(); @vkarpov15 Wouldn't it be neat if we made |
@AbdelrahmanHafez I already handle that like you say
but its gonna be extra excute and triky! |
The example in the referred-to-documentation uses The use case in the first comment (Document.prototype.populate) is that we have found a document, and later we found that we needed to populate some fields for that document. const board = await Game.findOne({ _id: someGame._id });
// here the API only supports execPopulate();
await board.populate({ path: '_p1' }).execPopulate(); Using Model.populate() however, supports chaining, and is a thenable. // notice Story is a model, not a document
Story.
find(...).
populate('fans').
populate('author') Please notice that mongoose queries are thenables, but are not real promises. Read more here. Also, now that I have given it some though, I don't think we can make Document.prototype.populate thenable without introducing a breaking change. |
@AbdelrahmanHafez Its good to have populated as the prototype in future but for now, I prefer this line of code to docs
|
I believe it's already present in the documentation
|
execPopulate()
to populate docs
We have an issue to track this change: #3834 . We'll make it so that |
@AbdelrahmanHafez why does Document.populate() have to return a promise ? |
@Houssem12-ai Please refer to #3834 |
mongoose Not able to return populated
see blow
Model
usage
expectation as return
in case using
const pop = await board.populate({ path: '_pl', select: '_n', options: { retainNullValues: true } })
it return only refrence id like below
working fin on call back
but on call back
await x.populate({ path: '_pl', select: '_n' }, console.log)
working fineDo you want to request a feature or report a bug?
Its a bug i think
What is the current behavior?
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
nodejs: 10
"mongoose": "^5.9.2",
db version v4.2.3
The text was updated successfully, but these errors were encountered: