我有一个包含以下字段的文档的集合:
description
state
field_num
我想将光标返回到满足以下标准的所有文档:
此查询是否正确?
DBObject query = new BasicDBObject("$gte",99)
.append("status","complete")
.append("description", new BasicDBObject("$not", ".*abc")))
DBCursor cursor = collection.find("collection name", query, projection)
答案 0 :(得分:0)
此查询:
状态"完成"
和field_num大于100
并且他们的描述不包含" abc"图案?
...可以表达如下:
Bson query =
// where field_num > 100
new BasicDBObject("field_num", new BasicDBObject("$gte", 100))
// where status is ' complete'
.append("status", new BasicDBObject("$eq", "complete"))
// where description does not contain 'abc'
// note: this uses inverse matching since the $not operator
// is not allowed with the $regex operator
.append("description", new BasicDBObject("$regex", "^((?!abc).)*$"));
在Java驱动程序的版本中> 3.0这也可以更简单地表达为:
Bson query= Filters.and(
Filters.gt("field_num", 100),
Filters.eq("status", "complete"),
Filters.regex("description", "^((?!abc).)*$")
);
查询执行如下:
MongoClient mongoClient = ...;
MongoCollection<Document> collection = mongoClient.getDatabase("...")
.getCollection("...");
collection.find(query)