i have a problem with mongoDB, im trying to create trigger, that will take server date and compare it with property DueDate and if the DueTime is lesser or equal of the server time, it should swap property borrowed to false.
Problem is that it didnt work and im so lost i tried everything.
There is my trigger function:
exports = function(changeEvent) {
const mongo = context.services.get("MongoDB");
const now = new Date();
const booksLended = mongo.db("test").collection("bookslendeds");
var filter = {DueDate: {$lt: now.toISOString()}, Borrowed: true};
var update = {$set: {Borrowed: false}};
console.log(JSON.stringify(filter));
console.log(JSON.stringify(update));
return booksLended.updateMany(filter, update);
};
This is a console logs:
> ran on Wed Jan 18 2023 23:48:10 GMT 0100 (Central European Standard Time)
> took 524.689137ms
> logs:
{"DueDate":{"$lt":"2023-01-18T22:48:11.778Z"},"Borrowed":true}
{"$set":{"Borrowed":false}}
> result:
{
"matchedCount": {
"$numberInt": "0"
},
"modifiedCount": {
"$numberInt": "0"
}
}
> result (JavaScript):
EJSON.parse('{"matchedCount":{"$numberInt":"0"},"modifiedCount":{"$numberInt":"0"}}')
CodePudding user response:
The datatype of the DueDate
field is probably UTC datetime, while .toISOString()
is returning a string. MongoDB query operators are type-sensitive, so these will not match.
Instead, use the date object directly in the query, like:
var filter = {DueDate: {$lt: now}, Borrowed: true};