Basic operation of MongoDB with insert document (documents)

Source: Internet
Author: User
Tags bulk insert mongodb client mongodb update

A. MongoDB basic operation:

1. View all current Databases:show dbs for MongoDB

2. Select database: Use DatabaseName (the database does not exist is automatically created, but only in the MongoDB preset cache, if you do not do anything, close the database, then the database will not exist, if you add data to the library, Then the library is really created.)

3. View a collection in the Database (collection): Show collections

4. Get help in MongoDB: Db.help () There are a lot of practical operations that are helpful when you don't know what MongoDB is doing.

5. Delete database: Use DatabaseName (must first select the database to be deleted), Db.dropdatabase ()

6. Delete the following collection from the specified database: Use DatabaseName (You must first select the database to delete the collection), Db.collectionName.drop ()

Two. MongoDB Insert Document

Because our MongoDB uses JavaScript to do the shell, it can be said that MongoDB client programming is similar to JavaScript

The document inserted into the collection is in the same format as the JSON, so the format of the document stored in the collection is Bson,bson, which is a binary form of the JSON-like format, called binary JSON

There are two ways to insert a new document into a collection, one is insert (), one is save ()

Difference:

Insert () Method: When the data is inserted, the _id data already exists, MongoDB will directly error, and return the corresponding _id

Save () method: When inserting data, it first loops through all the data in the collection, determines whether the _id of the inserted data exists in the collection, inserts a new document directly if it does not exist, and, if so, calls the MongoDB Update () method to update the _id corresponding data ( Equivalent to the content of the document covering the _id)

Grammar:

Db.collectionName.insert (document)

Db.collectionName.save (document)

  Attention:

1. Before inserting data, it is important to note that MongoDB is very sensitive to the data types of field (fields), such as when you insert an age field with a content of 30, which is a string type, but if you query with an integer type, This data is not found because the data type is not correct, so be careful when inserting the data, consider the type of the good field

2. Although MongoDB is very flexible, not like a relational database, the establishment of a new field, all the data has this field, but in the actual development, I still suggest you insert the data as far as possible to keep each set of fields are the same, so as to facilitate your development in the program

1. Insert the document method ()

    

    

As you can see, when you insert a piece of data successfully, it will return the result of the write, insert successfully, insert 1 [Writeresult ({"ninserted": 1})], and you will find that each piece of data preceded by a "_id", it is not the same as the relational database we use, The relational database we use generally primary key is an ID, and it's self-increasing (1,2,3,4 ...), and it's a unique identifier generated for each document in MongoDB, but it's unique, but it's not as self-growing as a relational database. Of course, we can also insert the specified "_id" (and then say Save () to practice again)

A. When we insert a piece of data, this "_id" already exists, what happens?

      

B. Of course the insert () method can also insert _id (custom _id values instead of MongoDB-generated _id), specifying that its value is the same as our relational database (1,2,3,4,5,6 ...).

         

C. Can mongodb be like a relational database in bulk INSERT? relational database BULK INSERT: INSERT INTO tablename (columns) VALUES (Text1), (Text2), (TEXT3) ..., And our MongoDB is not bulk inserted, only through the loop to achieve BULK insert

      

D. The field for each document in MongoDB can be different (mongodb flexibility, but not recommended)

      

2. Save () to insert the document method:

As with the Insert () method, the only difference is that if _id exists, save () updates the data for this _id, and insert is a direct error and returns the _ID

    

can also be batch update, while the non-existent is inserted, the presence of the update (_ID only three-way, through the cycle of updating the _id, but also added to the 4,5 of the two data):

    

From the above example, we can find:

1.mongodb does not need to define collection in advance, it automatically creates a collection when you insert data for the first time

2. In collection each document is added, unless we specify _id ourselves, the system generates a unique identity objectid

3. In collection, each document can have different field (field)

If there is a wrong place, welcome to criticize, I novice, beginner!!!!!!

Basic operation of MongoDB with insert document (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.