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.
asked Jul 27, 2018 at 7:13
Ankit
1,0211 gold badge13 silver badges31 bronze badges
2 Answers 2
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").
answered Jul 27, 2018 at 7:18
rrrr-o
2,5163 gold badges25 silver badges54 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
db.collection.update({_id :ObjectId("5aded60f6e89ea0e60e00e59"), "mail.messages.message" : "xyz"},
{$set: {"mail.messages.$.view": 1}
false, true);
answered Jul 27, 2018 at 7:22
Saurabh Mistry
13.8k5 gold badges60 silver badges78 bronze badges
3 Comments
Ankit
I have to do this on timestamp basis not by message. that is 'mail.messages.timestamp' : 'ISODate("2018年07月25日T10:51:15.915Z")'
Saurabh Mistry
please update ur question , in your question , u have mentions to update where message is xyz
Ankit
Updated the question. Please go through it
lang-js