[英]How can I check if particular value exists using Java driver 3.6+ mongodb?
I have documents like: 我有类似的文件:
My query is Document query = new Document("sent", new Document("$exists", true));
我的查询是Document query = new Document("sent", new Document("$exists", true));
Now I can check only if field exists, but I want to check if exists value of this field. 现在,我只能检查该字段是否存在,但是我想检查该字段是否存在值。
For example: if my document has field and value sent:true
, then I need to do something. 例如:如果我的文档具有sent:true
字段和值,那么我需要做些事情。
As I understand, it would similar to this: 据我了解,它类似于:
Document query = new Document(...);
if(query) {
//to do something
}
To check if particular value exists, you can use method count() with $eq operator: 要检查是否存在特定值,可以将方法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
}
If you want to check just existing field than you need to use $exists operator: 如果要只检查现有字段,则需要使用$ 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.