Ankit
Ankit

Reputation: 1011

Update array of specific object under an object Mongodb

I have a dataset like this:

{
    "_id" : ObjectId("5aded60f6e89ea0e60e00e59"),
    "mail" : {
        "count" : 1,
        "messages" : [ 
            {
                "message" : "xyz",
                "timestamp" : ISODate("2018-07-25T10:51:15.915Z"),
                "view" : 0
            },
            {
                "message" : "abc",
                "timestamp" : ISODate("2018-07-26T10:51:15.915Z"),
                "view" : 0
            }
        ]
    }
}

Now I have to update the View from 0 -> 1 on first object that is "timestamp": "ISODate("2018-07-25T10:51:15.915Z")".

For this I am writing the code but it did not work. Here is my code:

db.Collection.updateOne({'_id': ObjectId("5aded60f6e89ea0e60e00e59"), 'mail.messages.timestamp': 'ISODate("2018-07-25T10:51:15.915Z")'}, {$set: {'mail.messages.$.view': 1}})
.then(data => {
    console.log(data)
})
.catch(error => {
    console.log(error)
}) 



//I run this query on my mongodb shell and there it works perfectly. But when i write same query on my node js then it returns the error that is "typeError: callback.apply is not a function. This error arises when i mention false, true on my query at last i.e 
db.Collection.updateOne({"_id": ObjectId("5aded60f6e89ea0e60e00e59"), "mail.messages.timestamp": "2018-07-26T10:51:15.915Z"}, {$set: {"mail.messages.$.view": 1}}, false, true)

Anyone suggest me that where I am doing wrong on not appropriate. Any help is really Appreciated.

Upvotes: 0

Views: 41

Answers (2)

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

db.collection.update({_id :ObjectId("5aded60f6e89ea0e60e00e59"), "mail.messages.message" : "xyz"}, 
        {$set: {"mail.messages.$.view": 1}
        false, true);

Upvotes: 0

rrrr-o
rrrr-o

Reputation: 2516

You look to have single quotes around your dates in your find. So 'ISODate("2018-07-25T10:51:15.915Z")' is just being used as a string, it should just be ISODate("2018-07-25T10:51:15.915Z").

Upvotes: 1

Related Questions