Rajat Seeddharth
Rajat Seeddharth

Reputation: 307

Push into an array of documents in mongodb

Which operator to use to push a new question into array of question?I have got the document in following format.Which mongo db operator to use to insert a new question into array of questions provided that i am given chapterId and subchapterId in advance.

For example i want to insert a new question in subchapterId "1".

  {

    "chapterId": "38",
    "subChapter": [
      {

        "subchapterId": "1",
        "questions": [
          {

            "title": "Either his however modern. Stop central owner color type out. Interview five beyond interesting type suddenly.",

          },
          {

            "title": "Amount himself foreign color moment gun together sit. Deal race range heart despite several. Rather activity eat dinner save mission western. Civil past public enter four then.",

          },
          {

            "title": "Four former operation. Class continue away treatment.\nResponsibility condition dinner realize everything. Sign scene order quality yet. Within sing statement skill popular southern whole."

          },
          {

            "title": "Where of coach nature ask page allow.\nType exist hotel time. Central site policy everyone safe. Official administration family somebody.",

          },
          {

            "title": "Necessary dark these much region. Form sometimes seek. Future according detail piece section.\nNear everything admit. Senior Republican draw as expert market.",

          }
        ]
      },
      {

        "subchapterId": "2",
        "questions": [
          {

            "title": "Audience still use group. Yourself building police. Play imagine serious reality population reach.\nHerself without member must think concern window finish."

          },


          {

            "title": "Rule trip manage still. Imagine religious above race something successful.\nOnce base American series. Low page quite allow. Customer maybe base leave way under blood.",

          },
          {

            "title": "Church audience anyone garden. Federal when individual style.Value billion morning need box whether. Coach traditional cold each us truth.",

          }
        ]
      }
    ]
  }

Upvotes: 1

Views: 1140

Answers (3)

muthukumar selvaraj
muthukumar selvaraj

Reputation: 1073

use $addToSet Which avoids the Duplicates in the Array of objects while updating

db.yourcollectionName.update({        
  "subChapter.subchapterId" :"1"
},{$addToSet:{"subChapter.$.questions":{title:"updated Object"}}})

Upvotes: 0

Manish Sharma
Manish Sharma

Reputation: 31

You can use $push along with update

Example code is for chapterId:38 and subchapterId:1

db.collection_name.update({ "chapterId":"38", "subChapter.subchapterId": "1" }, { $push: { "subChapter.$.questions":{'title':'Newly added record'} }})

Upvotes: 0

Ashh
Ashh

Reputation: 46441

You can try this

const title = "this is the new question"

db.collection.update(
  { "subChapter.subchapterId": "1" },
  { "$push": { "subChapter.$.questions": { title } }}
)

Upvotes: 1

Related Questions