8

I have a document in mongodb collection like this:

{
   _id: 133,
   Name: "abc",
   Price: 20
}

I would like to add a new field "PackSizes" which may be or not may be of an array type, and then would like to an embedded document in it. Like-

PackSizes:
         [
             {_id: 123, PackSizeName:"xyz", UnitName:"pqr"}
         ]

or,

PackSizes:  {_id: 123, PackSizeName:"xyz", UnitName:"pqr"}

I'm a newcomer to mongodb. Please help.

s.k.paul
  • 7,099
  • 28
  • 93
  • 168

2 Answers2

15

You can do it with

db.test.update(
   { _id : 133 },
   { $set : { PackSizes:  {_id: 123, PackSizeName:"xyz", UnitName:"pqr"}} }
)

PackSizes could be any document, with array or without it.

Your result document will be

{
    "_id" : 133,
    "Name" : "abc",
    "Price" : 20,
    "PackSizes" : {
        "_id" : 123,
        "PackSizeName" : "xyz",
        "UnitName" : "pqr"
    }
}

Updated: For add new field and a member to array,

Assume we have your original document

{
   _id: 133,
   Name: "abc",
   Price: 20
}

Step 1 : add new field: PackSizes is an array

db.test.update(
   { _id : 133 },
   { $set : {PackSizes: [ {_id: 123, PackSizeName:"xyz", UnitName:"pqr"}]}}
)

Step 2: push new item to array

db.test.update(
   { _id : 133 },
   { $push : { PackSizes: {_id: 124, PackSizeName:"xyz", UnitName:"pqr"}} }
)

and you will have

{
    "_id" : 133,
    "Name" : "abc",
    "Price" : 20,
    "PackSizes" : [ 
        {
            "_id" : 123,
            "PackSizeName" : "xyz",
            "UnitName" : "pqr"
        }, 
        {
            "_id" : 124,
            "PackSizeName" : "xyz",
            "UnitName" : "pqr"
        }
    ]
}
Disposer
  • 6,201
  • 4
  • 31
  • 38
  • Disposer thanks. Would you please tell me how can i add a new PackSize to the PackSizes[] field? – s.k.paul Nov 17 '14 at 07:46
  • db.test.update( { _id : 133 }, { $push : { PackSizes: {_id: 124, PackSizeName:"xyz", UnitName:"pqr"}} } ) – Disposer Nov 17 '14 at 07:50
  • It creates an error- The field 'PackSizes' must be an array but is of type Object .... – s.k.paul Nov 17 '14 at 07:55
  • You should use $set command with PackSizes: [ {_id: 123, PackSizeName:"xyz", UnitName:"pqr"} ] then use $push command as I said. I tested it ;) – Disposer Nov 17 '14 at 07:58
  • I don't know this, sorry. Please add it as a complete command? – s.k.paul Nov 17 '14 at 08:01
  • 1
    skipping "step 1", and using "push" right away works for me too. "push" creates a new array for me if there isn't one already – Zhenya Jun 08 '17 at 08:34
1

In your Json structure _id is mongoDB immutable field so in your case if you changed _id to simply id and _id represents mongo id then following javascript may solve your problem

db.test.find().forEach(
           function(doc){ 
      db.upsert.update({},{"$unset:{"id":1,"Name":1,"Price":1}},false,true);
      db.upsert.update({},{"$set":{"PackSizes":doc}},true,false)}
       )

If you achieve your output as given by you then first you should unset your documents and then set update

Neo-coder
  • 7,715
  • 4
  • 33
  • 52