Skip to content
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

Model.populate() results in array, even if single ObjectID is being populated when using 'skipInvalidIds' option #8157

Closed
Hyperblaster opened this issue Sep 12, 2019 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Hyperblaster
Copy link

Hyperblaster commented Sep 12, 2019

When calling Model.populate() on a document or an array of documents, Mongoose returns the populated path as an Array.

The expected behaviour would be that if the path's value is a single ObjectId, String or Object (using the skipInvalidIds property) it should return a single Document, and if the value is an array then to return an Array of documents.

Schema

var PhotoSchema = new Schema({
    title: {
        type: String,
    },
    data: {
        type: Schema.Types.Mixed,
    },
});

var Photo = mongoose.model('photo', PhotoSchema);

Example Document

{
  "title":"Example",
  "data":{
     "photographer":{
           "_id":"5a9390c84028396d2a374432",
           "title":"John Smith",
     }
  }
}

Code to Populate

var items; //Array of documents containing the example document above

return Photo.populate(items, {
          path:"data.photograpger",
          select: controller.basicFields,
          skipInvalidIds: true, 
}, done);                        

Current Output

{
  "title":"Example",
  "data":{
     "photographer":[
     {
           "_id":"5a9390c84028396d2a374432",
           "title":"John Smith",
     }
     ]
  }
}       

Expected Output

{
  "title":"Example",
  "data":{
     "photographer":{
           "_id":"5a9390c84028396d2a374432",
           "title":"John Smith",
     }
  }
}       

This is really in relation to the skipInvalidIds feature, as if the objects are depopulated when saved to the database everything works as expected, It's just for when there are objects in the database that aren't plain ObjectIds but are objects with an _id that cause the issue.

My understanding was that when using the 'skipInvalidIds' feature the data would stay the same as it originally was before calling populate(), It's seems like it's a bit smarter than that and is actually populating the object picking up the _id property. Which is nice, it just needs to understand whether it's a single ID or an array of multiple IDs when it returns

@Hyperblaster Hyperblaster changed the title Model.populate() results in array, even if single ObjectID is being populated Model.populate() results in array, even if single ObjectID is being populated when using 'skipInvalidIds' option Sep 13, 2019
@vkarpov15
Copy link
Collaborator

You can work around this by setting the justOne option:

var items; //Array of documents containing the example document above

return Photo.populate(items, {
          path:"data.photograpger",
          select: controller.basicFields,
          skipInvalidIds: true,
          justOne: true 
}, done);    

The fact that you need to set justOne here is a bug that we will fix. Just suggesting it as a workaround.

@vkarpov15 vkarpov15 added this to the 5.7.2 milestone Sep 19, 2019
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Sep 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants