[英]How to use $and operator in mongodb - java driver
我们如何使用逻辑AND($ and)查询mongo,
示例:集合:主题
从收集主题中,我需要获取所有满足条件的记录,
totalmarks!= 10和totalmarks!= 15。
与Java驱动程序等效。
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));
另外,对于较新的版本,可以将Bson对象用作过滤器。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.