1

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:

  • Which way is better?
  • Is it better to use filters? What about the performance between doing the query is the first way against using a filter?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.