user3925365 user3925365 - 1 year ago 64
Java Question

MongoDB "find" query with multiple query documents

I am using MongoDB 3.2 with Java. I read the documentation and it says to use

since other options like
are deprecated. Now, I have query similar to:

db.schools.find({ zipcode: "63109" },
{ students: { $elemMatch: { school: 102 } } } )

I am wondering: how can I write this query in Java?

Note: Here we have two documents inside the
function, while it accepts only single
Bson Document
or multiple
Bson Element

Any help would be appreciated.

Answer Source

Try to use one document for the condition, like db.schools.find({ zipcode: "000000", students: { $elemMatch: { school: 102 }});


So, you are using Projection. In java mongodb driver 3.3 there are: public DBCursor find(DBObject query, DBObject projection). I think you should update your java mongodb driver.