ernirulez
ernirulez

Reputation: 751

mongodb sql in statement equivalent

I have found two ways of doing an equivalent of SQL in statement in mongodb. One way would be something like:

BasicDBObject inQuery = new BasicDBObject();
List<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(4);
list.add(5);
inQuery.put("employeeId", new BasicDBObject("$in", list));
DBCursor cursor = collection.find(inQuery);

And the other one, with a filter, would be something like this:

FindIterable<Document> iterable = db.getCollection("coll_name")
            .find(in("field_name", values))

My questions are:

Upvotes: 1

Views: 509

Answers (1)

Philipp
Philipp

Reputation: 69663

Performance-wise and semantically there is no difference.

Both result in the same command being sent to the database. The Filters.in(field, values) method is just an alternative syntax for new BasicDBObject(field, new BasicDBObject("$in", values)).

Which one you consider better from a code-stylistic point of view is your personal preference.

Upvotes: 1

Related Questions