toadead
toadead

Reputation: 1098

Same Field Used Twice in MongoDB Query

So I'm trying to understand the difference between 2 queries. I got different counts.

First query is

db.getCollection("my_col").find({
    modifiedDate: { $gte: new ISODate("2019-02-03") },
    modifiedDate: { $lte: new ISODate("2019-02-09") },
}).count()

Second query is

db.getCollection("my_col").find({
   modifiedDate: {
       $gte: new ISODate("2019-02-03"), 
       $lte: new ISODate("2019-02-09")
   }
}).count()

Can someone please help me understand why I got different counts for these 2 queries?

Upvotes: 2

Views: 354

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311865

The keys of a JavaScript object must be unique, so:

db.getCollection("my_col").find({
    modifiedDate: { $gte: new ISODate("2019-02-03") },
    modifiedDate: { $lte: new ISODate("2019-02-09") },
}).count()

becomes:

db.getCollection("my_col").find({
    modifiedDate: { $lte: new ISODate("2019-02-09") }
}).count()

Upvotes: 2

Related Questions