Home » MongoDB

Creating a collection in MongoDB and show the list of existing one’s

In this article, we are going to learn about the collections and the commands to create and show the list of existing collections in MongoDB.
Submitted by Manu Jemini, on February 02, 2018

In this example, we are going to create a collection and to show following existing collections of a particular database (here vehicle).

As doing above thing we need to execute following instructions and follow certain process to instantiate queries.

Process to run server of mongo

1) We need to go in the directory of mongo and type cmd in URL search box, and then press enter. A command prompt opens up in that particular directory like this,

MongoDB - Creating a collection 1

2) Then type mongod and press enter, then it seems like this:

MongoDB - Creating a collection 2

As we are all set with our mongo server,

3) Now we change our directory inside bin and open anther command prompt by typing cmd in URL search bar and then, type command mongo.

4) After open mongo shell we just need to open a database to create a collection in.

5) To show database list you just need to type command show dbs, and there is a list of existing databases shown in prompt,

MongoDB - Creating a collection 3

6) Now choose /create a database and use it like this,

MongoDB - Creating a collection 4

7) Now for creating a collection you need to type command db.createCollection("collection name") and after pressing enter a collection of your given name will made up inside the database.

MongoDB - Creating a collection 5

8) You can make more than one collections inside a single database by using similar command like,

MongoDB - Creating a collection 6

9) After that you have two collections in your database name cars and bikes, to show them you just need to type show collections command and press enter and there is a list comes out with your existing collections,

MongoDB - Creating a collection 7


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.