1

I have a document in mongodb, like this one:

{ "_id" : "a", "array" : [ { "aa" : "11", "bb" : "22" } ] }

I want to insert an array in the array above, so the result looks like:

{"_id":"a","array":[{"aa":"11","bb":"22","cc":[{mm:"11"},{mm:"22"}]}]}

I am writing a node.js program and using the mongojs module to connect to mongodb. I have tried using db.collection.aggregate({"$unwind":"$name"}), but I don't know how to solve this.

2
  • 1
    did you try using $push to add the array field ?db.collection.update( { _id: "a" }, { $push: { array: {[{mm:"11"},{mm:"22"}]}}}) Commented Dec 12, 2016 at 16:05
  • 1
    The Aggregation framework is mean for reading data, but you appear to want to write to the document. Can you clarify exactly what you are looking to do? Do you want to add cc to the array in the document in the database or only during a read? Commented Dec 12, 2016 at 16:26

1 Answer 1

1

If you want to update document then used update with push as below

db.collectionName.update(
  {"_id":"a"},
  {
    "$push":{
      "array":{
      "cc":[
        {"mm":"11"},
        {"mm":"22"}
      ]
    }
  }
})

Or if you want to add new field in aggregation then use project with literal as below

db.collectionName.aggregate({
  "$project":{
    "array":{
      "$map":{
        "input":"$array",
        "as":"el",
        "in":{
          "aa":"$$el.aa",
          "bb":"$$el.bb",
          "cc":{
            "$literal":[
              {"mm":"11"},
              {"mm":"22"}
            ]
          }
        }
      }
    }
  }
}).pretty()
Sign up to request clarification or add additional context in comments.

5 Comments

My only concern here is $project doesn't actually add to the document in the database, just to the document returned. It is essentially ephemeral.
@PeteGarafano So for this I was created two different query if OP want to update document then update work if want only in aggregation then aggregation ;), because OP not mentioned here to update document or not ?
I agree, OP's needs are unclear, I have commented asking for clarification
@Yogesh As PeteGarafano comment, 'Aggregation' is mean for reading, howerver as my code showed, I want to write the document. but if I type your code, the mongo console show: { "_id" : "a", "array" : [ { "aa" : "11", "bb" : "22" }, { "cc" : [ { "mm" : "11" }, { "mm" : "22" } ] } ] } what I mean is {"_id":"a","array":[{"aa":"11","bb":"22","cc":[{mm:"11"},{mm:"22"}]}]} I want to insert a array in first item of the array in the document. thanks your answer~
@all thanks everyone, I already solve this question with db.course.update({_id:1},{"$addToSet":{"arr.0.content":"ok"}}) Again thank you Yogesh and PeteGarafano time to reply my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.