Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Mixing populated query results with model objects #570

Closed
DaBlick opened this Issue · 5 comments

5 participants

@DaBlick

I have a Schema sorta like this (abbreviated here):

var Comment = module.exports = new Schema({
  , teacher :   { type: Schema.ObjectId, index: true, ref: 'User'}

var StudentProfile = module.exports = new Schema({
  , comments : [Comment]

Here's my code to add a new comment. You can presume that std contains the result of a query like

       db.model('StudentProfile').findById(id).populate("comment")

And now I execute this code:

        var comment = new Comment;
        comment.teacher    = ~~~ some User object

        std.comments.push(comment);

        std.save(function (err) {...}

        res.render('reporting/_comment', {
            'comments' : std.comments,

Assume there is already one Comment and I'm pushing a second one.

Here's what std.comments look likes at the render call

   [ {teacher : {User object}},
     {teacher : id of User object} }   <--- Newly pushed comment

What you can see here is that the pre-existing Comment from the query result is populated, but the new one is not.

With the advent of the populate feature this kind of scenario is not going to be uncommon and it would be quite nice if the model objects the mongoose magic that turns comment.teacher back into an id was transparent.

@athleticus

I've encountered this issue as well, and it is mighty frustrating. Any word on a fix?

@aheckmann
Owner

relates to #601

@pykler

If there was a manual_populate method, it might solve some of these issues ... another solution would be instead of having:

comment_obj.teacher = teacher_obj

Automagically just keep the teacher_obj._id and leave the whole obj ... it would keep the full teacher_obj but when saving checks the schema and then only saves the teacher_obj._id at that point.

@braunsquared

+1 Needs to be consistent. Either store the field as an id always or an object. Preference would be an id, with a call to a populate like call that returns the object if present.

@aheckmann aheckmann referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann populate; allow adding docs to populated arrays
without casting

need to test string,number,buffer _ids yet

part of #570
3e51d6c
@aheckmann aheckmann referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@aheckmann aheckmann referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann populate; allow adding docs to populated arrays
without casting

need to test string,number,buffer _ids yet

part of #570
8601989
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann populate; fix saving populated arrays
when null or ObjectId was added

relates to #570
4bed277
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann tests; adding docs to populated arrays
add tests for docs with String, Buffer, and Number _ids

relates to #570
63db499
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann populate; depopulate populated single paths
also fixes legacy issue where single populated path
was markedModified causing populated document to
overwrite _id.

relates to #570
8a60a88
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann populate; allow adding docs to populated arrays
without casting

need to test string,number,buffer _ids yet

part of #570
fc545ef
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann populate; fix saving populated arrays
when null or ObjectId was added

relates to #570
089e9e8
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann tests; adding docs to populated arrays
add tests for docs with String, Buffer, and Number _ids

relates to #570
b0c646e
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann populate; depopulate populated single paths
also fixes legacy issue where single populated path
was markedModified causing populated document to
overwrite _id.

relates to #570
0856b82
@aheckmann aheckmann referenced this issue from a commit
@aheckmann aheckmann fixed; setting populated paths
Setting a populated path (or manipulate a populated array) with
works when the schema path does not have a `ref` declared and
an adhoc model was used for population.

relates to #570
87b2a33
@aheckmann
Owner

fixed in 3.6

@aheckmann aheckmann closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.