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.