So this is Day 3 of learning Mongo Db. I'm coming from the MySql universe...
A lot of times when I need to write a query for a MySql table I'm unfamiliar with, I would use the "desc" command - basically telling me what fields I should include in my query.
How would I do that for a Mongo db? I know, I know...I'm searching for a schema in a schema-less database. =) But how else would users know what fields to use in their queries?
Am I going at this the wrong way? Obviously I'm trying to use a MySql way of doing things in a Mongo db. What's the Mongo way?
MongoDB creates collections automatically when you insert some documents. For example: Insert a document named seomount into a collection named SSSIT. The operation will create the collection if the collection does not currently exist. If you want to see the inserted document, use the find() command.
To get stats about MongoDB server, type the command db. stats() in MongoDB client. This will show the database name, number of collection and documents in the database.
We can get the schema object/first document of the collection using : var schemaObj = db. users. findOne();
The explain command provides information on the execution of the following commands: aggregate , count , distinct , find , findAndModify , delete , mapReduce , and update . Although MongoDB provides the explain command, the preferred method for running explain is to use the db. collection.
Type the below query in editor / mongoshell
var col_list= db.emp.findOne(); for (var col in col_list) { print (col) ; }
output will give you name of columns in collection :
_id name salary
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With