Du kan bruge $exists
operator i kombination med .
notation. Den blotte forespørgsel i mongo-skallen skulle se sådan ud:
db.yourcollection.find({ 'otherInfo.text' : { '$exists' : true }})
Og en testcase i Java kunne se sådan ud:
BasicDBObject dbo = new BasicDBObject();
dbo.put("name", "first");
collection.insert(dbo);
dbo.put("_id", null);
dbo.put("name", "second");
dbo.put("otherInfo", new BasicDBObject("text", "sometext"));
collection.insert(dbo);
DBObject query = new BasicDBObject("otherInfo.text", new BasicDBObject("$exists", true));
DBCursor result = collection.find(query);
System.out.println(result.size());
System.out.println(result.iterator().next());
Output:
1
{ "_id" : { "$oid" : "4f809e72764d280cf6ee6099"} , "name" : "second" , "otherInfo" : { "text" : "sometext"}}