MongoDB Java驱动程序-在查找查询中使用
我想获取不存在字段下载的所有文档MongoDB Java驱动程序-在查找查询中使用,java,mongodb,bson,mongo-java,Java,Mongodb,Bson,Mongo Java,我想获取不存在字段下载的所有文档 find{ "download" : {$exists: false}} 对于Java,我发现了一个示例: BasicDBObject neQuery = new BasicDBObject(); neQuery.put("number", new BasicDBObject("$ne", 4)); DBCursor cursor = collection.find(neQuery); while(cursor.hasNext()) {
find{ "download" : {$exists: false}}
对于Java,我发现了一个示例:
BasicDBObject neQuery = new BasicDBObject();
neQuery.put("number", new BasicDBObject("$ne", 4));
DBCursor cursor = collection.find(neQuery);
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
我的改编是
BasicDBObject field = new BasicDBObject();
field.put("entities.media", 1);
field.put("download", new BasicDBObject("$exists",false));
System.out.println("Start Find");
DBCursor cursor = collection.find(query,field);
System.out.println("End Find Start Loop ALL 100k");
int i = 1;
while(cursor.hasNext())
现有行不工作:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: com.mongodb.MongoException: Unsupported projection option: $exists
at com.mongodb.MongoException.parse(MongoException.java:82)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:314)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
at com.mongodb.DBCursor._check(DBCursor.java:368)
at com.mongodb.DBCursor._hasNext(DBCursor.java:459)
at com.mongodb.DBCursor.hasNext(DBCursor.java:484)
at ImgToDisk.main(ImgToDisk.java:61)
... 5 more
现在还不知道应该采用哪种方法,因为我的查询是在shell中运行的,并且使用了UMongo,所以到java的转换似乎不太容易看到
collection.find(query,field);
find方法的第二个参数DBObject用于指示要返回的结果文档的哪些属性。它有助于减少网络负载
在您的示例中,尝试将$exists子句放入字段
DBObject。那不行。
第二个参数对象的属性值只能为1(包括此属性)或0(排除)
将其放入名为query
的第一个DBObject中
另请参见和您可以使用DBCollection通过JavaAPI实现这一点
public List<BasicDBObject> Query(String collection, Map<String, String> query, String... excludes) {
BasicDBObject bquery = new BasicDBObject(query);
BasicDBObject bexcludes = new BasicDBObject(excludes.length);
for (String ex : excludes) {
bexcludes.append(ex, false);
}
return db.getCollection(collection, BasicDBObject.class).find(bquery).projection(bexcludes)
.into(new ArrayList<BasicDBObject>());
公共列表查询(字符串集合、地图查询、字符串…排除){
BasicDBObject bquery=新的BasicDBObject(查询);
BasicDBObject bexcludes=新的BasicDBObject(不包括.length);
for(字符串ex:excludes){
bexcludes.append(ex,false);
}
返回db.getCollection(collection,BasicDBObject.class).find(bquery).projection(bexcludes)
.into(新数组列表());
}
db是我们需要指定两件事 第一:
collection.find(query,field);
上述命令不是来自MongoDB Java驱动程序。
这意味着我们只能在mongo shell(或mongo Compass)中运行,而不能在java中运行
第二:
要限制java中的字段,可以像下面的代码一样使用projection()
find(queryFilter)
.projection(fields(include("title", "year"),exclude("_id")))
完整示例:
Bson queryFilter = eq("cast", "Salma Hayek");
//or Document queryFilter = new Document("cast", "Salma Hayek");
Document result =
collection
.find(queryFilter)
//.limit(1) if you want to limit the result
.projection(fields(include("title", "year"),exclude("_id")))
//or .projection(fields(include("title", "year"), excludeId()))
//or .projection(new Document("title", 1).append("year", 1))
.iterator()
.tryNext();
在find()调用中引用查询对象。将其包含在示例中可能会有所帮助