0

If I use Mongoose to run a query on a schema that has a reference to another model (in this case, a BlogPost model with a property author which references another Author model).

let posts = await BlogPost.find({}, "title author").populate("author","name bio").lean()

By passing lean() I am supposed to get a POJO back like this:

[{
    "_id": "602828eaff09bca9c3af2dbb",
    "author": {
        "_id": "602433bd98d380443ef9aeb7",
        "name": "Matt",
        "bio": "matt is cool"
    },
    "title": "Hello World",
    "__v": 0
}, {
    "_id": "6048b8cb0200e51b4f2486c8",
    "author": {
        "_id": "602433bd98d380443ef9aeb7",
        "name": "Matt",
        "bio": "matt is cool"
    },
    "title": "Another Blog post",
    "__v": 0
}]

However if I modify for example

posts[0].author.name = "Test"

then ALL the posts with this author in the array are modified:

[{
    "_id": "602828eaff09bca9c3af2dbb",
    "author": {
        "_id": "602433bd98d380443ef9aeb7",
        "name": "Test",
        "bio": "matt is cool"
    },
    "title": "Hello World",
    "__v": 0
}, {
    "_id": "6048b8cb0200e51b4f2486c8",
    "author": {
        "_id": "602433bd98d380443ef9aeb7",
        "name": "Test",
        "bio": "matt is cool"
    },
    "title": "Another Blog post",
    "__v": 0
}]

If instead I only wanted to affect the first object in the array, what should I do?

Matt
  • 412
  • 7
  • 17
  • 1
    They were referenced to same object, check more about shadow clone and deep clone at https://stackoverflow.com/a/12690181/7174186 before edit object – buithienquyet Mar 21 '22 at 06:30
  • indeed its happening because the authors reference the same object, i was wondering if there was anything inbuilt in Mongoose to say "I want a really plain object with no references" so I don't need to handle cloning subdocuments afterwards. – Matt Mar 21 '22 at 06:49
  • 1
    Did you try setting option "clone" for "pupulate"? https://github.com/Automattic/mongoose/issues/8760 – buithienquyet Mar 21 '22 at 07:09
  • indeed, that's exactly what i need! thanks. if you want to add that as a proper answer I can approve it. https://mongoosejs.com/docs/api/query.html#query_Query-populate [options.clone=false] «boolean» When you do BlogPost.find().populate('author'), blog posts with the same author will share 1 copy of an author doc. Enable this option to make Mongoose clone populated docs before assigning them. – Matt Mar 21 '22 at 09:29
  • I am happy when it can help you. Simply, you can up vote my comment. – buithienquyet Mar 21 '22 at 09:40

1 Answers1

0

The populate function in Mongoose https://mongoosejs.com/docs/api/query.html#query_Query-populate has an option clone:true

[options.clone=false] «boolean» When you do BlogPost.find().populate('author'), blog posts with the same author will share 1 copy of an author doc. Enable this option to make Mongoose clone populated docs before assigning them.

So the correct code would be something like

let posts = await BlogPost.find({}, "title author").populate({path:"author", fields:"name bio", clone:true}).lean()

Thanks to @buithienquyet in the comments for the pointers

Matt
  • 412
  • 7
  • 17