user3925365 user3925365 - 2 months ago 14
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

org.bson.BsonDocument
since other options like
BSONObject
and
Document
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
find
function, while it accepts only single
Bson Document
or multiple
Bson Element
(s).

Any help would be appreciated.

Answer

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

EDIT:

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.

Comments