Reputation: 25
I am trying to translate a working Mongo Shell query to java, but it seems like I have ran into a wall. I am using the mongo-java-driver:3.9.1. The shell query looks like this:
db.getCollection('xyz').find({source_id: ObjectId("abc"), timestamp: {
$gte : ISODate("2019-04-05 18:24:00.000Z"),
$lt: ISODate("2019-04-05 18:30:00.000Z")
}
})
The query includes two criterias: a matching source_id and timestamps between the two given dates.
My Java query looks like this (startDate and endDate are formatted Dates):
FindIterable<Document> cursor = collection.find(and(eq("source_id", new ObjectId("abc"))), and(gte("timestamp", startDate), lt("timestamp", endDate)));
Intellij gives me the message: "Cannot resolve method 'find(org.bson.conversions.Bson, org.bson.conversions.Bson)". Is this a logical error, or is building a filter like that not possible?
Upvotes: 0
Views: 136
Reputation: 3491
You are missing one and
:
FindIterable<Document> cursor = collection.find(and(and(eq("source_id", new ObjectId("abc"))), and(gte("timestamp", startDate), lt("timestamp", endDate))));
The general idea is: think of every {
as an encompassing and
statement.
Upvotes: 1