1

I have the below document stored in mongo DB collection,I will need to add new subscribers For eg.,I need to add the subscriber with "protocol" : "SOAP" and url http://localhost.8080/FNOL/subscriber3 for the name "name" : "FNOL","country" : "US","lob" : "property" from the document.

If the url we are adding already exist we shouldn't add to the document,in case if the document does not exist with matching criteria name "name" : "FNOL","country" : "US","lob" : "property" i would need to insert a new document.

is it possible to do all the above in a single command in mongo db?

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 updation:

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

                    {
                            "protocol" : "SOAP",
                            "url" : "http://localhost.8080/FNOL/subscriber2"
                    },
                    {
                            "protocol" : "JMS",
                            "url" : "NOTIFICATION.TOPIC.FNOL"
                    },
                    {
                            "protocol" : "SOAP",
                            "url" : "http://localhost.8080/FNOL/subscriber2"
                    }
        ,
                    {
                            "protocol" : "SOAP",
                            "url" : "http://localhost.8080/FNOL/subscriber3"
                    }   
            ]
    }
Ashh
  • 44,693
  • 14
  • 105
  • 132
Ravi
  • 1,247
  • 4
  • 15
  • 35
  • Note that `$addToSet` will still actually add the entry if the `url` exists but the `protocol` is different. Therefore it's not actually be best option here. Use `$ne` to test for the presence of the value in the array instead for that case – Neil Lunn May 30 '18 at 01:47

2 Answers2

1

You need to use $addToSet... It will not push the data if it already exist in the array

db.collection.update(
  { name: 'FNOL', country: 'US', lob: 'property' },
  { $addToSet: { subscribers: { "protocol" : "SOAP", url: 'http://localhost.8080/FNOL/subscriber3' } } },
  { upsert: true }
)
Ashh
  • 44,693
  • 14
  • 105
  • 132
  • Hi Ashish,Thanks it works for the scenario when the subscriber does not exist and it exists as expected,but when there is no match { name: 'FNOL', country: 'US', lob: 'property' } i would also need to add a new document.is it possible with single statement? – Ravi May 29 '18 at 16:46
  • check the updated answer... upsert true will do this – Ashh May 29 '18 at 16:48
  • worked like a charm.I have accepted the answer.By the way iam newbie to Mongo DB.Is there any quickway i can master mongo db?? – Ravi May 29 '18 at 16:50
  • yes indeed it is ;) – Ravi May 29 '18 at 16:56
0

Try this.

db.collection.update( { "_id": (id of the object) }, { "$push": {"subscribers": { "url": "(input url )", "protocol": "(input protocol)" } } } )