Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-49812

How to upsert an element in mongodb array?

    XMLWordPrintableJSON

Details

    • Icon: Question Question
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None
    • None

    Description

      Suppose our document looks like this

      {
         a:1,
         b:[
          {c:120,d:100},
          {c:121,d:110}
         ]
      }
      

      Now how could I upsert new objects in this array? Suppose I want to perform update on the above document and add {c:200,d:120} to b so my expected result looks like this

      {
         a:1,
         b:[
          {c:120,d:100},
          {c:121,d:110},
          {c:200,d:120}
         ]
      }
      

      Also the update will of $inc, meaning suppose I want to increment d by 200 if c is present(lets say c is 200 and it is already present in the above document), if not present then I want to upsert the document itself.

      Attachments

        Activity

          People

            jonathan.streets@mongodb.com Jonathan Streets (Inactive)
            shubham08singh10@gmail.com Shubham Singh
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: