Reputation:
How do we query for mongo using logical AND($and),
Example : collection : Subject
From collection subject I need to get all the records satisfying the condition,
totalmarks!=10 and totalmarks!=15.
Upvotes: 5
Views: 8682
Reputation: 125
Alternatively, for newer versions, you can use Bson object as a filter.
mongoDatabase = mongoClient.getDatabase("DBName");
MongoCollection<Document> col = mongoDatabase.getCollection("CollectionName");
Bson filter = Filters.and(
Filters.ne("totalmarks", 5),
Filters.ne("totalmarks",15));
MongoCursor<Document> cursor = col.find(filter).iterator();
Upvotes: 2
Reputation: 1400
Equivalent in java driver.
List<DBObject> criteria = new ArrayList<DBObject>();
criteria.add(new BasicDBObject("totalmarks", new BasicDBObject("$ne", 10)));
criteria.add(new BasicDBObject("totalmarks", new BasicDBObject("$ne", 15)));
DBCursor dbCursor = collection.find(new BasicDBObject("$and", criteria));
Upvotes: 9