MongoDB Java Driver - Use exists projection in find query
Solution 1
you use
collection.find(query,field);
the DBObject that is the 2nd parameter to the find method is used to indicate which attributes of the resulting documents you want to be returned. It is useful for reducing network load.
In your example, you try to put an $exists clause into the field
DBObject. That won't work.
The 2nd parameter object can only have attribute values of 1(include this attribute) or 0(exclude).
Put it into the first DBObject called query
instead.
Solution 2
you can use DBCollection to do that with JAVA API
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>());
}
db is MongoDataBase
Solution 3
We need to specify two things.
First :
collection.find(query,field);
The above command is not from MongoDB Java driver. That means we can run only in the mongo shell (or in the mongo Compass) and not in the java.
Second: to limit the fields in java you can use projection() like the following code
find(queryFilter)
.projection(fields(include("title", "year"),exclude("_id")))
Complete Example:
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();
Dukeatcoding
Updated on June 28, 2022Comments
-
Dukeatcoding almost 2 years
I want to get all documents where the field download does not exists
find{ "download" : {$exists: false}}
For Java I found an Example:
BasicDBObject neQuery = new BasicDBObject(); neQuery.put("number", new BasicDBObject("$ne", 4)); DBCursor cursor = collection.find(neQuery); while(cursor.hasNext()) { System.out.println(cursor.next()); }
My Adaption is
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())
The Exists Line is not working:
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
It have no clue right now which the right adaption would be, since I got my query working in the shell and with UMongo, the transfer to java seems to be not so easy to see.