MongoDB Create Collection

So as we know that data stored in mongodb as document and documents stored in the form of collection and that collection is stored in database.

Create Collection Syntax :

The syntax of the Create Collection is −
1.The best thing about MongoDB is that you do not need to create an collection before inserting a document into it. With one command you can insert a document into the collection and MongoDB creates that collection on the fly.

2.We can also create a collection before actually inserting data into it. This method gives you options that you can set when creating a collection.

 
   
\\Creating the Collection in MongoDB on the fly

db.collection_name.insert({key:value, key:value…})

-----------------------------
-----------------------------

\\Creating collection with options before inserting the documents

db.createCollection(name, options)


Create Collection Example :

The Example of the Create Collection is −

\\Example of Creating the Collection in MongoDB on the fly

> use mydb
switched to db mydb

db.mydb.insert({
  name: "Rahul",
  age: 25,
  website: "letsfindcourse.com"
})


-------------------
-------------------


\\Example of Creating collection with options before inserting the documents

> db.createCollection("students")
{ "ok" : 1 }
 






Visit :


Discussion


* You must be logged in to add comment.