I have the following query which I am trying to implement in Java (I am using 3.4.2 mongo-java driver) :
我有以下查询,我试图在Java中实现(我使用3.4.2 mongo-java驱动程序):
Json:
{
"_id" : "Team:2334918",
"fieldName" : [
"Delivery",
"Support"
],
"isDeleted" : false
}
Here the query:
这里查询:
db.myCollection.find({$nor: [{“fieldName”: {$exists: false}},{“fieldName”:{$size:0}}]})
I have written the following code to implement the above query:
我编写了以下代码来实现上述查询:
MongoClient mc = ctm.getMongoConn();
db = ctm.getDatabase(mc);
col = db.getCollection(collectionName);
BasicDBObject searchObject = new BasicDBObject();
List<BasicDBObject> searchArguments = new ArrayList<BasicDBObject>();
searchArguments.add(new BasicDBObject("fieldName",new BasicDBObject("$exists",false)));
searchArguments.add(new BasicDBObject("fieldName",new BasicDBObject("$size",0)));
searchObject.put("$nor", searchArguments);
MongoCursor<Document> curs = (MongoCursor<Document>) col.find(searchObject);
while (curs.hasNext()){
Document doc = curs.next();
Object name1 = doc.get("fieldName");
System.out.println(name1.toString());
j++;
}
I am getting the following error while running:
运行时出现以下错误:
java.lang.ClassCastException: com.mongodb.FindIterableImpl cannot be cast to com.mongodb.client.MongoCursor
2 个解决方案
#1
1
Replace this:
MongoCursor<Document> curs = (MongoCursor<Document>) col.find(searchObject);
while (curs.hasNext()) {
}
With this:
FindIterable<Document> docs = col.find(searchObject);
for (Document d : docs) {
}
The suggestion from @Veeram (in the comments above) is correct too.
来自@Veeram的建议(在上面的评论中)也是正确的。
#2
1
Try
MongoCursor<Document> curs = col.find(searchObject).iterator();
to get cursor.
MongoCursor
You don't have to get hold of cursor. You can use the methods provided in the FindIterable
which iterates the cursor for you. example at the end.
你不必掌握光标。您可以使用FindIterable中提供的方法为您迭代光标。最后的例子。
Also you are mixing Document
(3.x) and BasicDBObject
(2.x). You can replace your search builder with
您还混合了Document(3.x)和BasicDBObject(2.x)。您可以使用替换搜索构建器
Filters searchObject = Filters.nor(Filters.exists("fieldName"), Filters.size("fieldName", 0);
FindIterable<Document> curs = col.find(searchObject);
curs.forEach();
More info here by the author.
更多信息,作者在这里。
#1
1
Replace this:
MongoCursor<Document> curs = (MongoCursor<Document>) col.find(searchObject);
while (curs.hasNext()) {
}
With this:
FindIterable<Document> docs = col.find(searchObject);
for (Document d : docs) {
}
The suggestion from @Veeram (in the comments above) is correct too.
来自@Veeram的建议(在上面的评论中)也是正确的。
#2
1
Try
MongoCursor<Document> curs = col.find(searchObject).iterator();
to get cursor.
MongoCursor
You don't have to get hold of cursor. You can use the methods provided in the FindIterable
which iterates the cursor for you. example at the end.
你不必掌握光标。您可以使用FindIterable中提供的方法为您迭代光标。最后的例子。
Also you are mixing Document
(3.x) and BasicDBObject
(2.x). You can replace your search builder with
您还混合了Document(3.x)和BasicDBObject(2.x)。您可以使用替换搜索构建器
Filters searchObject = Filters.nor(Filters.exists("fieldName"), Filters.size("fieldName", 0);
FindIterable<Document> curs = col.find(searchObject);
curs.forEach();
More info here by the author.
更多信息,作者在这里。