MongoDB -Collection Operations
Usually we don’t need to create collection. MongoDB creates collection automatically when you insert some documents.
Example: Insert a document named -admin” into a collection named -users”. The operation will create the collection if the collection does not currently exist
> db.users.insert({"username":"admin", "password":"Admin@123"})
WriteResult({ "nInserted" : 1 })
> show collections
users
We can also create collection by using db.createCollection(name, options)
1. Create Collection
Syntax: db.createCollection(name, options)
-
Name: is a string type, specifies the name of the collection to be created.
-
Options: is a document type, specifies the memory size and indexing of the collection. (optional)
> db.createCollection("books") { "ok" : 1 }
2.check the collections in the database
Syntax: show collections()
> show collections
books
users
3. Drop Collection
Syntax: **db.
> db.books.drop();
true
> show collections
users
The drop command returns true if it successfully drops a collection. It returns false when there is no existing collection to drop.
PREVIOUSDatabase Operations