-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"
                    }
            ]
    }
kevinadi
  • 13,365
  • 3
  • 33
  • 49
Ravi
  • 1,247
  • 4
  • 15
  • 35

1 Answers1

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 }}})
mickl
  • 48,568
  • 9
  • 60
  • 89
  • Thanks it seems to work with mongo shell.I will try with camel mongo component and get back to you on this. – Ravi May 29 '18 at 16:22