Hi @Dev_INX - Welcome to the community
I inserted the test document into my test environment and did the query using the official MongoDB Node Driver (Version 4.8.1).
Here’s a snippet of the query portion of the code I had used against my test environment:
async function run() {
await client.connect();
console.log("Connected correctly to server");
const db = client.db(dbName);
const coll = db.collection("coll");
try {
const query = await coll.find({ 'updatedAt': {
$gt: new Date("2022-01-01"), //.toISOString(),
$exists: true
}
},
{
allowDiskUse: false
}).toArray()
console.log(query)
}
catch (e) {
console.dir(`Failed to drop collection: ${e}`);
}
}
I have passed through a value of new Date("2021-01-01")
to the $gt
operator as shown above and tried to keep the query portion as close to what you had provided as possible which did return a result below.
The output:
Connected correctly to server
[
{
_id: new ObjectId(“128dec47bbd2f73014646ed1”),
firstName: ‘first’,
lastName: ‘last’,
email: ‘dev+simulator1@mail.net’,
emailVerified: true,
passwordCreated: 2022-10-06T09:50:03.488Z,
apiRequestStatus: {
maxNumOfKeys: 20,
status: ‘UNSET’,
createdAt: 2022-10-06T09:50:03.488Z,
updatedAt: 2022-10-06T09:50:03.488Z
},
state: {
isFrozen: false,
adminEmail: null,
updatedAt: 2022-10-06T09:50:03.488Z
},
platform: ‘XXX’,
lastLogin: 2022-10-06T09:50:03.488Z,
accountType: ‘XXX’,
beneficialOwnerName: null,
enableTFA: false,
isReviewed: false,
popupToShow: [],
marketingRefferal: { referralCode: ‘CCCC-CCCC’, advocateReferralCode: ‘DDDD-DDDD’ },
isMigratedFromS: false,
password: ‘...’,
__v: 0,
createdAt: 2022-10-06T09:50:03.555Z,
updatedAt: 2022-10-06T09:50:05.555Z
}
]
Query in the code does not return expected result, it returns nothing.
Regarding the following in your code:
updatedAt = dateToMongoSearch(updatedAt);
Could you log this value and advise the output including the data type? Please also review the Date() documentation in regards to the new Date()
function.
If you still require further assistance, please provide the following:
updatedAt
variable value in your code when it is executed
- Driver version
- MongoDB Server Version
Regards,
Jason