1

Imagine I have database like

{
    "_id" : ObjectId("594994d1ab6c161168aa5969"),
    "user_id" : ObjectId("594994d1ab6c161168aa5968"),
    "active" : [ 
        {
            "type" : "active"
        }, 
        {
            "type" : "inactive"
        }
    ]
}

I want add to first object in active array . Here is result I expected

{
    "_id" : ObjectId("594994d1ab6c161168aa5969"),
    "user_id" : ObjectId("594994d1ab6c161168aa5968"),
    "active" : [ 
        {
            "type" : "active",
            "note": [{
                            content: 'here is content',
                            title : ' here is title'
                        }]
        }, 
        {
            "type" : "inactive"
        }
    ]
}

Here is code I tried

db.collection('..').update({'user_id' : ObjectId(userId)} ,
{$push:{ "active.0": "note": [{
                              content: 'here is content',
                                title : ' here is title'
                        }]  } )

But I get The field 'active.0' must be an array but is of type object in document . Where is my wrong ? Please help

6
  • 1
    Should be {$push:{ "active.0.note": { content: 'here is content', title : ' here is title' } } } Commented Jun 20, 2017 at 8:13
  • @NeilLunn I tried this and it create new field active[0] in db Commented Jun 20, 2017 at 8:19
  • @NeilLunn I want add it to first object of array active Commented Jun 20, 2017 at 8:20
  • Nope that did not. Your previous statement did. You need to remove that from the document and reset to what you had before. Commented Jun 20, 2017 at 8:21
  • @NeilLunn What you mean is I should remove active array ? Commented Jun 20, 2017 at 8:24

1 Answer 1

2

Starting with your document like this:

{
    "_id" : ObjectId("594994d1ab6c161168aa5969"),
    "user_id" : ObjectId("594994d1ab6c161168aa5968"),
    "active" : [ 
        {
            "type" : "active"
        }, 
        {
            "type" : "inactive"
        }
    ]
}

You run the $push like this:

db.collection.update(
  { "_id" : ObjectId("594994d1ab6c161168aa5969") },
  { "$push":{ "active.0.note": { content: 'here is content', title : ' here is title' } } }
)

Which creates the new array within the first element like so:

{
        "_id" : ObjectId("594994d1ab6c161168aa5969"),
        "user_id" : ObjectId("594994d1ab6c161168aa5968"),
        "active" : [
                {
                        "type" : "active",
                        "note" : [
                                {
                                        "content" : "here is content",
                                        "title" : " here is title"
                                }
                        ]
                },
                {
                        "type" : "inactive"
                }
        ]
}

Everything here is cut and paste from my shell.

Sign up to request clarification or add additional context in comments.

Comments

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.