开发者

Exists Query for multilple Document in Mongodb using java driver

开发者 https://www.devze.com 2023-04-04 14:20 出处:网络
If we want to check that the record is exists in Collection or not, then there is an operator $exists in开发者_如何学Python Mongodb. But if we want to know multiple records exists in Collection then h

If we want to check that the record is exists in Collection or not, then there is an operator $exists in开发者_如何学Python Mongodb. But if we want to know multiple records exists in Collection then how can we check that in single query using java driver?

For Example I have two document: {"key": "val1"} {"key": "val2"}

Now if I want to check that 'val1' and 'val2' is exist or not then how can we do that in single query using java driver? Note: here field name is same in both the documents.


You need to use $in operator for that

 db.collection.find( { key : { $in : ['val1','val2'] } } );

equivalent java code might like this

List<string> values = new ArrayList<string>();
values.add("val1")
values.add("val2")
BasicDBObject query = new BasicDBObject();
query.put("key", new BasicDBObject("$in", values));
DBCursor cursor = yourcollection.find(query);

am not much of a java guy, this is going to be more or less same.

0

精彩评论

暂无评论...
验证码 换一张
取 消