Get schema information in MongoDB

Posted By : Akash Sharma | 01-May-2013

In this blog I am going to share the steps for getting information of schema of a collection.

Let say we have a collection named “posts” and we want to get names of fiels in all documents in that collection.

> mr = db.runCommand({ "mapreduce" : "posts", "map" : function() { for (var key in this) { emit(key, null); } },"reduce" : function(key, stuff) { return null; } , "out": "posts" + "_keys"});

{

      "result" : "posts_keys",

      "timeMillis" : 441,

      "counts" : {

              "input" : 1000,

              "emit" : 8000,

              "reduce" : 80,

              "output" : 8

      },

      "ok" : 1

}

> db[mr.result].distinct("_id")

[

      "_id",

      "author",

      "body",

      "comments",

      "date",

      "permalink",

      "tags",

      "title"

]

> 

The output of second command gives names of all fields in “posts” collection.

 

Akash Sharma

[email protected]

About Author

Author Image
Akash Sharma

Akash is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Akash loves playing Cricket and Tennis

Request for Proposal

Name is required

Comment is required

Sending message..