MongoDB Learning Notes (introductory tutorial) series 2-Inserting documents

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.