This section will take a series of operations on how to insert a document into MongoDB.
Suppose we have a MongoDB service on this machine that listens on port 8866.
Then first use: MONGO--port 8866 (equivalent to MONGO 127.0.0.1:8866) command into the MongoDB shell
The general system automatically creates several databases: Local,test, and so on, when you connect using the MONGO command, the test database is selected by default.
Tips
Use the DB command to view the current database.
Use the show DBS command to view all databases in the current MongoDB service.
You can switch databases by using use XXX.
So let's first insert a piece of data in test:
Db.coll.save ({"Name": "David", "Age": 26})
Well, the data is inserted in the Coll collection under the test database. The Coll collection is automatically created, and the collection resembles a table in a relational database. In general, the collection does not have to be created separately, it can be assumed that the collection already exists when inserting the data, and if it does not exist, the system is created automatically. The rules for naming and file names for collections are similar.
After the insert is complete, you can use show collections to view all the collections under test. Use "Db.coll.find ()" To view all the data in the Coll collection under test. The whole process:
When each piece of data is inserted, a "_id" field is automatically added as the ID of the data, which can be specified by itself. In fact, in general, you need to control the "_id" field, otherwise the data is much more troublesome.
For example, when we insert a data that is exactly the same as the one we have just now without specifying "_id", there is a situation:
Except the ID is different, the others are the same!
OK, finally, we introduce a few delete commands:
Delete a collection: db. Xxxx.drop (), XXXX is the collection name
Delete Current database: Db.dropdatabase ()
MongoDB Learning Notes (introductory tutorial) series 2-Inserting documents