Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I describe a collection in Mongo?

Tags:

mongodb

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?

like image 276
Cavachon Avatar asked Jun 13 '11 22:06

Cavachon


People also ask

How can you define a collection in MongoDB?

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.

How do I view data collection in MongoDB?

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.

How do I find the schema of a collection in MongoDB?

We can get the schema object/first document of the collection using : var schemaObj = db. users. findOne();

What is explain command in MongoDB?

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.


1 Answers

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 
like image 174
Bharathiraja Avatar answered Sep 23 '22 18:09

Bharathiraja