0

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.

0

2 Answers 2

1

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").

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

Comments

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

3 Comments

I have to do this on timestamp basis not by message. that is 'mail.messages.timestamp' : 'ISODate("2018-07-25T10:51:15.915Z")'
please update ur question , in your question , u have mentions to update where message is xyz
Updated the question. Please go through it

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.