Mongodb entry-5 insert

Source: Internet
Author: User
Mongodb entry-5 insertion is essential for each database to add, delete, modify, and query data. First, we will introduce add, that is, insert. To compare the four operations, insert is the simplest in mongodb. The insret command is used to insert data in mongodb. Before using this command, we will first introduce operations such as creating databases and viewing collections. Create a database in mo

Mongodb entry-5 insertion is essential for each database to add, delete, modify, and query data. First, we will introduce add, that is, insert. To compare the four operations, insert is the simplest in mongodb. The insret command is used to insert data in mongodb. Before using this command, we will first introduce operations such as creating databases and viewing collections. Create a database in mo

Mongodb entry-5 insert

For each database, addition, deletion, modification, and query are essential. First, we will introduce add, that is, insert.

To compare the four operations, insert is the simplest in mongodb. The insret command is used to insert data in mongodb. Before using this command, we will first introduce operations such as creating databases and viewing collections.

Create a database

Use the use database name in mongodb to create a new database, but remember that after using this command, mongodb will not create a database immediately, the database can be viewed only after data is inserted into it. Let's look at the following example:

[Html]

> Show dbs

Local 0.078125 GB

> Use new

Switched to db new

> Db

New

> Show dbs

Local 0.078125 GB

>

In the above Code, we used the shwo dbs command to view the current database. We can see that although we switched the database to new, we still cannot view the database using show dbs. We continue to write the above program:

[Html]

> Db. c1.insert ({name: "xiangyue "})

> Show dbs

Local 0.078125 GB

New 0.203125 GB

>

At this time, we can see the new database. For the set, when there is no data, it cannot be seen. The view command of the set is:

[Html]

Show tables

Show collectios

Insert data

Two insert methods are provided in mongodb: insert and save.

We can see that both inser and save can be inserted successfully, and the _ id key is automatically added to the inserted document. This can be understood as the primary key of the document, it is automatically generated by mongodb.

The difference between save and insert is that if the _ id of the inserted data is the same, save will update this document, and insert will report an error.

In this example, we insert a document with _ id 3. When we insert the same document with insert, an error is returned. However, we succeeded in using save. We checked the document and mongodb updated it for us. The find method used here is to view all the documents in the Set, which will be detailed later.

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.