3

I've been doing some search but haven't been able to find the answer. From docs, In mongo, if

$match: { type: "airfare"}

translated as:

DBObject match = new BasicDBObject("$match", new BasicDBObject("type", "airfare") );

in Mongo Java driver, how to translate this one into Mongo java driver?

$match : { score : { $gt : 70, $lte : 90 } }

EDIT

This is what I actually wanted to do:

$match : { bookingDateTime: { $gte : fromDate, $lte : toDate } }

And this is how implemented in mongodb java driver, with no luck so far:

DBObject matchFields2 = new BasicDBObject("$match", new BasicDBObject("bookingDateTime", new BasicDBObject("$gte", fromDate.getTime()).append("$lte", toDate.getTime())));
DBObject match2 = new BasicDBObject("$match", matchFields2);

fromDate is a Calendar object so hence the getTime() method to convert Calendar class to Date class.

1 Answer 1

3
Object match = new BasicDBObject("$match", 
   new BasicDBObject("score",
   new BasicDBObject("$gt", 70).append("$lte", 90) ) )
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Anyway, i tried to do this for querying match by date:
But it did not work and always return empty values. The code can be seen on the edited section.
can you cross check that if there are documents matching this query, run the command in your mongo shell. db.coll.find({ bookingDateTime : {$gte : fromData, $lte : toDate} })
yes, it found some documents using the following query: db.BookingJob.find({ bookingDateTime : {$gte : start, $lte : end} }); where var start = new Date(2013, 6, 12, -16, 00, 00); and var end = new Date(2013,6, 12,7, 59, 59);
DBObject matchFields2 = new BasicDBObject("$match", new BasicDBObject("bookingDateTime", new BasicDBObject("$gte", fromDate.getTime()).append("$lte", toDate.getTime()))); DBObject match2 = new BasicDBObject("$match", matchFields2); you don't have to use match2, use the first variable itself i.e. db.getCollection('coll').aggregate(matchFields2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.