'How to compare mongodb Object ids?

To compare two mongo document ids I'm using the following query in nodejs.

  _id: { $gt: mongoose.Types.ObjectId(another_id) }

But what I need is initially I want to retrieve all the records from the collection. For that what should I pass it to another_id. For example, initially I tried passing null, "", and 0 . query would look like below,

 _id: { $gt: mongoose.Types.ObjectId(null) }
 _id: { $gt: mongoose.Types.ObjectId("") }

But these fails. Can you please suggest me, what should I pass initially in another_id to retrieve all the records in the collection.

I'm trying to assign it to a variable and attaching it to query as below, but it is not working. Please suggest.

    let pagqy = {};
    if(bookmrk !== 0){
        pagqy = {_id: { $gt: mongoose.Types.ObjectId(bookmrk)}}  
    }
     if (fromdate == "" && todate == "") 
            query = { Class: param,pagqy } 
     else
          query = { E_Assigned_Date: { $gte: fromdate, $lte: todate }, Class: param,pagqy }
        


Solution 1:[1]

use this query to compare mongo _id

_id: mongoose.Types.ObjectId(another_id)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jitendra chauhan