Studio 3T visual query builder: timestamp operations on mongoDB ObjectId

52 Views Asked by At

Using the visual query builder is very simple.
However I cannot figure out how to do timestamp queries on ObjectIds.

Basically I want to be able to do this:

3T studio visual query builder, query on ObjectId timestamp

In code this would look like this:

const ObjectId = require('mongodb').ObjectId;

// Calculate the timestamp for January 1, 2023, in seconds
const timestamp = Math.floor(new Date('2023-01-01T00:00:00Z').getTime() / 1000);

// Create an ObjectId with the calculated timestamp and 0s for the machine and increment values
const objectIdReference = new ObjectId(timestamp, 0, 0, true);

// Query documents with ObjectId greater than the reference ObjectId
db.collection('yourCollectionName').find({ _id: { $gt: objectIdReference } }).toArray(function(err, docs) {
    if (err) {
        console.error(err);
        return;
    }

    // Handle the result documents
    console.log(docs);
});

Is there any built-in functionality in Studio-3T to do that behind the scenes?

0

There are 0 best solutions below