MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Come recuperare un valore da mongoDB, in base al suo nome chiave?

find() è sufficiente.

db.CollectionName.find({},{HomeTown:1})

in Java, sarebbe:

BasicDBObject query = new BasicDBObject();
BasicDBObject field = new BasicDBObject();
field.put("HomeTown", 1);
DBCursor cursor = db.getCollection(collectionName).find(query,field);
while (cursor.hasNext()) {
    BasicDBObject obj = (BasicDBObject) cursor.next();
    result.add(obj.getString("HomeTown"));
}