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

0

1 Answer 1

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}]}
);
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.