0

I have this data:

_id : 1
status:1, 
name:"name",
details:
{ 
  crm:115, 
  webs: 
    { tag:"blog" , url:"http://..."}, 
  contacts:
    {
      _id:1,
      name:"me", 
      phones:
        { tag:"home", number:"123..." },
        {tag:"mobile", number:"123456789"}
    }
}

I want one more entry in "phones" with {tag:office", number:"9823..."}

What would be the command/query for that?

5
  • 2
    It's not clear what you're asking, can you clarify your question? Commented Jul 11, 2013 at 12:27
  • Please do not refer to another question. Create a seperate, stand-alone question with a clear question, a clear schema and the problem you are unable to solve. Thanks ;) Commented Jul 11, 2013 at 12:41
  • Here is it, the above mentioned is my final question, sorry for the previous one. :) Commented Jul 11, 2013 at 12:46
  • 1
    Please show the code that you've tried. It looks like you want phones to be an array, so, you'd need to use $push: docs.mongodb.org/manual/reference/operator/push Commented Jul 11, 2013 at 12:50
  • db.abcd.update( { _id : 1 }, { $push: {details: { contacts: { phones: { tag:"office", rname:"9823" } } } } } ); Commented Jul 11, 2013 at 13:03

2 Answers 2

2

You can easily push this into the array with the following query (I had to modify the JSON you pasted, as it was not valid a little):

db.collection.drop();
db.collection.insert( {
    _id : 1,
    status: 1, 
    name: "name",
    details: { 
        crm:115, 
          webs: {
            tag:"blog",
            url:"http://..."
        }, 
        contacts: {
            _id: 1,
            name: "me", 
            phones: [
                { tag: "home", number: "123..." },
                { tag:"mobile", number:"123456789" }
            ]
        }
    }
} );

db.collection.update(
    { _id: 1 },
    { $push : { 'details.contacts.phones' : {  tag:"office", rname:"9823" } } }
);

db.collection.find().pretty();
{
    "_id" : 1,
    "details" : {
    "contacts" : {
        "_id" : 1,
        "name" : "me",
        "phones" : [
            {
                "tag" : "home",
                "number" : "123..."
            },
            {
                "tag" : "mobile",
                "number" : "123456789"
            },
            {
                "tag" : "office",
                "rname" : "9823"
            }
        ]
        },
        "crm" : 115,
        "webs" : {
        "tag" : "blog",
        "url" : "http://..."
        }
    },
    "name" : "name",
    "status" : 1
}
Sign up to request clarification or add additional context in comments.

4 Comments

When I do so I get error "Cannot apply $push/$pushAll modifier to non-array" :(
I actually tested with the document that you showed above. I've updated my example with the insert and find+results too.
Ok, Thank you so much for that, but suppose if I want to add new entry in contacts say, {_id:2,name:"hitesh"}.. Then, for that the following doesn't work and why? db.collection.update( { _id: 1 }, { $push : { 'details.contacts' : { _id:2, name:"hitesh" } } } );
That's because contacts is not an array, but just a simple document. It needs to be an array if you want to push into it. However, if you turn that into an array, then the original push to add a phone number no longer works. You can only have one of them to work. In your case, you probably want to split out contacts+phone into a different collection with a link back to the main document.
0

The value of the $push operator must refer to the array to be updated. So when the array field is embedded in other documents you need to use dot notation like this:

db.abcd.update({_id: 1}, 
    {$push: {"details.contacts.phones": {tag:"office", rname:"9823"}}});

3 Comments

Thats ok with arrays, but I haven't used arrays, so how do I query to push data in "phones", is there any other way ?
@Hitesh Ok, I assumed phones was an array as what you show isn't a valid doc. Can you fix phones in your example doc to show what you mean?
When I do so I get error "Cannot apply $push/$pushAll modifier to non-array" :(

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.