-1

I have the below document stored in mongo DB collection,I will dynamically receive the url to be removed For eg.,I need to delete the subscribers url http://localhost.8080/FNOL/subscriber1 for the name "name" : "FNOL","country" : "US","lob" : "property" from the document.

How do i write the remove command with mongo?

Do i need to redefine my document structure?

Thanks in advance.

{
        "_id" : ObjectId("5b07fbbc0d7a677d2f8b2d87"),
        "name" : "FNOL",
        "country" : "US",
        "lob" : "property",
        "subscribers" : [
                {
                        "protocol" : "REST",
                        "url" : "http://localhost.8080/FNOL/subscriber1"
                },
                {
                        "protocol" : "SOAP",
                        "url" : "http://localhost.8080/FNOL/subscriber2"
                },
                {
                        "protocol" : "JMS",
                        "url" : "NOTIFICATION.TOPIC.FNOL"
                }
        ]
}

After removal:

{
            "_id" : ObjectId("5b07fbbc0d7a677d2f8b2d87"),
            "name" : "FNOL",
            "country" : "US",
            "lob" : "property",
            "subscribers" : [

                    {
                            "protocol" : "SOAP",
                            "url" : "http://localhost.8080/FNOL/subscriber2"
                    },
                    {
                            "protocol" : "JMS",
                            "url" : "NOTIFICATION.TOPIC.FNOL"
                    }
            ]
    }
1
  • Using mongo shell? Commented May 29, 2018 at 16:09

1 Answer 1

2

You can use $pull operator specifying mentioned conditions to get matching document and url as a parameter of $pull like below:

let urlToRemove = "http://localhost.8080/FNOL/subscriber1";
db.col.update(
    { name: "FNOL", country: "US", lob: "property" }, 
    { $pull: { subscribers: {url: urlToRemove }}})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it seems to work with mongo shell.I will try with camel mongo component and get back to you on this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.