Before you start using MongoDB (version:3.2.9), you must first create Database and Collection in MongoDB. Database is independent, each database has its own collections, different database, the name of the same collection can exist, but the database is not a physical storage unit, MongoDB takes collection as the physical storage unit, each collection has its own data files and index files, which end with. Wt.
One, create collection
1. View the list of collection in the current database
Show collections
2, implicitly Create collection
In MongoDB, collection corresponds to the table of a relational database, and the user does not need to explicitly define collection to insert data into collection. MongoDB automatically creates collection when inserting data into collection for the first time, and if collection already exists in database, MongoDB inserts data directly into collection.
Db.foo.insert ({_id:1,name: "Test"})
3, explicitly create collection
Use Db.createcollection () to create collection explicitly, creating collection for a specific purpose by specifying collection Option.
Because MongoDB creates a collection implicitly when the collection was first referenced in a command, this method is used Primarily for creating new collections this use specific options.
For example, if you create a fixed collection (Capped Collection), the normal collection can grow automatically to accommodate more doc, but the fixed set has the largest size and the doc cannot exceed the limit (max option).
true, size:5242880, max:5000})
4, delete collection, call collection Drop method to delete collection
Db.collection_name.drop ()
Second, CREATE database
1. View the database list of MongoDB
Show DBS
View the database on which the current connection resides
Db
2. Create database using the use command
Use My_database_name
MongoDB returns the following information, the use command is only registered to MongoDB database, and is not actually created using show DBS view, the list does not have that database.
Switched to DB My_database_name
3, create a collection in the current database and insert data into the collection
Db.foo.insert ({_id:1,name: "Test"})
At this point, MongoDB really created database, looking at the folder where the data was stored, found two more. wt files, one for storing data, and one for storing index. Using Show DBS view, the database exists in the list.
Third, delete database
When you delete a database, you must be very careful, unless you are using a test environment.
1, using the use command, switch to the specified database
Use database_name
2, use the DB command to view the current database, avoid deleting the error
Db
3, delete the current database
Db.dropdatabase ()
Reference Documentation:
Db.createcollection ()
MongoDB Create Database and Collection