1
 {
        "_id" : ObjectId("5ec0471dfec11a07d80c9d07"),
        "name" : "jasper",
        "posts" : [
                {
                        "_id" : ObjectId("5ec0473ffec11a07d80c9d08"),
                        "content" : "It,s all about........",
                        "title" : "THE NEEDY"
                },
                {
                        "_id" : ObjectId("5ec0475afec11a07d80c9d09"),
                        "content" : "I know..........",
                        "title" : "The world"
                }
        ],
        "__v" : 2
}

There are many users and their data are stored in the form of above schema in MongoDB.I want to update any of the content (i.e consider"I know") then how should I do that using mongoose?? Firstly I tried to get the userId and would get its entire data, then how should I select the Id of posts and make necessary updates?

THANKS FOR YOUR HELP

Shrey S.
  • 23
  • 5

1 Answers1

0

Sounds like you want to use arrayFilters.

It would look a little something like:

db.collection.updateOne(
    {_id: userId},
    {
        $set: {
            "posts.$[post].content": newContent,
        }
    },
    {arrayFilters: [{"post._id": postId}]}
);
Tom Slabbaert
  • 21,288
  • 10
  • 30
  • 43
  • Thanks for the update section.Can u just please tell me what if i just don't want to update and just find that which post id the user is saying about? – Shrey S. May 22 '20 at 12:19