我有这样的文件:
我的query是Document query = new Document("sent", new Document("$exists", true));
现在我只能检查字段是否存在,但我想检查该字段是否存在。
例如:如果我的文档有字段和值sent:true
,那么我需要做一些事情。
据我了解,它类似于:
Document query = new Document(...);
if(query) {
//to do something
}
要检查是否存在特定值,可以将方法count()与$eq运算符一起使用:
long count = collection.count(new Document("sent", new Document("$eq", true)));
if (count < 5) { // if the number of documents less than 5 has value as true
// then to do something
}
如果您想检查现有字段而不是需要使用$exists运算符:
long count = collection.count(new Document("sent", new Document("$exists", true)));
if (count < 5) { // if the number of documents less than 5 has field named as sent
// then to do something
}