Learn MongoDB in 8 days-add, delete, query, and modify in the next day

Source: Internet
Author: User

After reading the previous article, I believe everyone will know how to enable mongodb. This article details the addition, deletion, query, modification, and so on. First, when we open mongodb in the same way as the previous article, suddenly

I was dumpfounded, but I couldn't turn it on. I carefully observed the "Dash area" and found a similar "lock file" under the db folder to prevent mongodb from being turned on, what we need to do next

If it is disabled, it is enabled successfully. The mongodb management method will be shared in subsequent articles.

 

I. Insert operation

The previous article also said that the document is stored in the "K-V" format, if you are familiar with JSON, I believe that learning mongodb is hand to hand, we know the Value in JSON

It may be a "string", an "array", or an embedded JSON object. The same method is also suitable for BSON.

There are two common insert operations: single insert and batch insert ".

① Single insert

Previously, the mongo command opened a javascript shell. Therefore, JavaScript syntax works in this way, and it looks like it is awesome.

② Batch insert

This is different from "single Insert". I believe you should know that because mongodb does not provide the "batch insert method" for shell, it doesn't matter. Drivers in various languages are interconnected.

With the batch insert method in mongodb, this method is indispensable. If you have to simulate batch insert, you can write the for loop by yourself, which is insert.

 

Ii. Find operation

In daily development, we play query, and the most frequently used is the second class:

①: >,>=,<,<= ,! =, =.

②: And, OR, In, NotIn

These operations are encapsulated in mongodb and will be described as follows:

<1> "$ gt", "$ gte", "$ lt", "$ lte", "$ ne", "no special keywords ", these are exactly the same as above. For example.

<2> "No keyword", "$ or", "$ in", "$ nin", I also give a few examples.

 

<3> there is a special match in mongodb, that is, "Regular Expression", which is very powerful.

 

<4> sometimes the query is very complicated and it hurts, but it doesn't matter. mongodb gave us a big offer. It is $ where. Why do we say this because the value in $ where

That is, we are very familiar with and love js to help us.

 

Iii. Update operations

Update operations are nothing more than two types. The overall update and partial update are also clear to everyone in use.

<1> overall update

I don't know, but you still remember that when I used update in the previous article, the update is actually an overall update.

<2> partial update

Sometimes we only need to update a field instead of the overall update. What should we do? The easy problem is that mongodb already provides us with two

Modifier: $ inc and $ set.

① $ Inc Modifier

$ Inc, short for increase, should be familiar to those who have learned SQL server. For example, if we make an online user status record, each modification will be based on the original

The value specified by auto-increment $ inc. If this key is not found in the document, a key is created. The following example shows how to use this key.

 

② $ Set Modifier

Don't talk about anything, just go to the code

 

<3> upsert operation

This is the word created by mongodb. Do you still remember that the first parameter of the update method is "query condition ?, This upsert operation means: If I

I just added a new entry to the database. In fact, this is also advantageous, that is, it avoids the use of the update or add operation in the database, which is very simple.

Set the third parameter of update to true.

<4> batch update

If multiple matching entries are found in mongodb, only the first entry is updated by default. If we need to update multiple entries in batches, it is very easy to implement in mongodb.

And set it to true in the fourth parameter of update. This is not an example.

 

4. Remove operation

This operation was simply said in the previous article. I will not go into details here.

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.