我想要获取不存在现场下载的所有文档
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())Exists线路不工作:
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似乎并不容易。
发布于 2013-04-09 21:03:47
您可以使用
collection.find(query,field);DBObject是find方法的第二个参数,用于指示要返回的结果文档的哪些属性。这对于减少网络负载非常有用。
在您的示例中,您尝试将$exists子句放入field DBObject中。那行不通的。第二个参数对象只能具有属性值1(包括此属性)或0(排除)。
将其放入名为query的第一个DBObject中。
另请参阅here和here
发布于 2015-06-09 21:22:38
您可以通过JAVA API使用DBCollection来实现这一点
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>());}
数据库为MongoDataBase
发布于 2019-12-17 06:43:41
我们需要指定两件事。
首先:
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();https://stackoverflow.com/questions/15902287
复制相似问题