Push into an array of documents in mongodb


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.",

}
]
}
]
}





Think this might help stackoverflow.com/questions/13987365/…
– Mamtha Soni K
Jul 3 at 4:16





Thank you very much.
– Ricky Sterling
Jul 3 at 4:23




3 Answers
3



You can try this


const title = "this is the new question"

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



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'} }})



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


$addToSet


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages