-4

With the driver Java Mongodb, I am looking for a way to return all fields of one collection.For example, I have a collection "people", how can I get all fields, I want output: 'id','name','city'...

C. Song
  • 41
  • 1
  • 3
  • Possible duplicate of [Query fields in a MongoDB Collection.](http://stackoverflow.com/questions/9639260/query-fields-in-a-mongodb-collection) – vijayst Aug 23 '16 at 03:21
  • http://stackoverflow.com/questions/2298870/mongodb-get-names-of-all-keys-in-collection – Breavyn Aug 23 '16 at 05:07
  • Thanks a lot, but I am trying to query all field name of a collection instead of specific fields. – C. Song Aug 23 '16 at 10:13

5 Answers5

3

Thanks a lot, I have finally got the answer.

DBCursor cur = db.getCollection("people").find();
DBObject dbo = cur.next();
Set<String> s = dbo.keySet();
menuka
  • 61
  • 8
C. Song
  • 41
  • 1
  • 3
1

From manual:

To return all documents in a collection, call the find method without a criteria document. For example, the following operation queries for all documents in the restaurants collection.

FindIterable<Document> iterable = db.getCollection("restaurants").find();

Iterate the results and apply a block to each resulting document.

 iterable.forEach(new Block<Document>() {
        @Override
        public void apply(final Document document) {
            System.out.println(document);
        }
   });
Istiak Morsalin
  • 10,621
  • 9
  • 33
  • 65
0

You'd need to decide on the number of samples that would make sense to you but this would pull the last 10 submissions.

Document nat = new Document().append("$natural",-1);
    FindIterable<Document> theLastDocumentSubmitted = collection.find().limit(10).sort(nat);

    ArrayList<String>fieldkeys = new ArrayList<String>();

    for (Document doc : theLastDocumentSubmitted) {
        Set<String> keys = doc.keySet();
        Iterator iterator = keys.iterator();
        while(iterator.hasNext()){
            String key = (String) iterator.next();
            String value = (String) doc.get(key).toString();
            if(!fieldkeys.contains(key)) {
                fieldkeys.add(key);
            }
        }
    }
    System.out.println("fieldkeys" + fieldkeys.toString());
Lee S
  • 1
0
  • Below code will return all fields from given collection.

     MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("collection_name");
                        AggregateIterable<Document> output = mongoCollection.aggregate(Arrays.asList(
                                new Document("$project", Document("arrayofkeyvalue", new Document("$objectToArray", "$$ROOT"))),
                                new Document("$unwind", "$arrayofkeyvalue"),
                                new Document("$group", new Document("_id", null).append("allkeys", new Document("$addToSet", "$arrayofkeyvalue.k")))
                        ));
    
Rahul Mahadik
  • 11,668
  • 6
  • 41
  • 54
-1

Below code will return all fields of people collection :

db.people.find()

  • This line returns a DBCursor object, then what should I do next? Use a method in DBCursor class? – C. Song Aug 23 '16 at 10:24