Skip to content

The method fromDate can't be found #1029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ajiankexx opened this issue Apr 26, 2025 · 1 comment
Open

The method fromDate can't be found #1029

ajiankexx opened this issue Apr 26, 2025 · 1 comment

Comments

@ajiankexx
Copy link

I can use such query command in NoSQL booster for MongoDB:

db.getCollection("_random_name").find({
    _id: { $gt: ObjectId.fromDate(new Date("2025-01-01T00:00:00Z")) }
})

However, in mongodb-js, the same command can't be run. It remind me that ObjectId.fromDate is not a function
I am using latest mongodb-js.

Thank you for your help.

@nirinchev
Copy link
Contributor

ObjectId.fromDate is a method that existed in the legacy mongodb shell, but is not something that was ever part of the driver, which is what the vscode extension uses under the hood. It does support createFromTime though, so you'd need to modify your code like:

db.getCollection("_random_name").find({
    _id: { $gt: ObjectId.createFromTime(new Date("2025-01-01T00:00:00Z") / 1000) }
})

The / 1000 is necessary because dates are represented in millisecond precision, while createFromTime expects a number in seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants