Linda
Linda

Reputation: 119

Multiple $and $or in a MongoDb find

I have a query I can't figure out. That query returns nothing:

 const read = await db.collection("action_traces")
    .find(
      { $and: [
        { $or:
          [
            {"receipt.receiver": "newdexpocket"},
            {"act.account": "newdexpocket"}
          ]
        },
        { $and:
          [
            {"block_time":{ "$gte": "2018-10-02T00:00:00.000Z" } },
            {"block_time":{ "$lt": "2018-10-03T00:00:00.000Z" } }
          ]
        }
      ]
    })

Whereas the following request returns part of the results I want:

 const read = await db.collection("action_traces")
    .find(
    { $and: [
      { $or:
        [
          {"receipt.receiver": "newdexpocket"},
          {"act.account": "newdexpocket"}
        ]
      },
      {"block_time":{ "$lt": 2018-10-03T00:00:00.000Z } }
    ]
  })

The issue is simple, how can get rid of the documents that are older than 2018-10-02T00:00:00.000Z? What am I doing wrong in my first request?

Many thanks in advance,

Upvotes: 0

Views: 60

Answers (1)

Bajal
Bajal

Reputation: 5986

The and between the critieria should be implied, the below query should be equivalent to what you're trying to achieve (if I understood it right). Please test it.

db.collection("action_traces")
.find({ $or:[
        {"receipt.receiver": "newdexpocket"},
        {"act.account": "newdexpocket"}
      ],
     "block_time":{ "$gte": "2018-10-02T00:00:00.000Z" ,"$lt": "2018-10-03T00:00:00.000Z" } 
})

Upvotes: 1

Related Questions