MongoDB Learning Summary-2 (detail and additions to the search and change)

Source: Internet
Author: User
Tags bulk insert modifier

Read the previous article, I believe everyone will know how to open MongoDB, this article on the next one of the additions and deletions to change, first when we use the same way to open MongoDB, suddenly

Dumbfounded, wipe, unexpectedly open, carefully observe the "line area" information, found that the DB folder has a similar "lock file" blocked the opening of MongoDB, next we have to do is

is to kill it, after that, the success of the management of MongoDB will be shared in subsequent articles.

One: 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 extremely easy, we know that the JSON value

It could be "string", maybe "array", and possibly an inline JSON object, and the same way is appropriate for Bson.

Common insert operations also exist in two forms: "Single insert" and "BULK insert".

① Single Insert

As I said earlier, the MONGO command opens a JavaScript shell. So JS syntax in this can work, it seems to be not very bull X.

② BULK Insert

The difference between this thing and the "single insert" I believe you should know that because there is no "bulk Insert method" provided to the shell in MongoDB, it doesn't matter, driver in each language

With MongoDB internal bulk insertion method, because this method is indispensable, if you want to simulate the next batch insert, you can write a for loop, inside is insert.

Two: Find operation

In the daily development, we play the query, play the most is two categories:

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

②:and,or,in,notin

These operations are encapsulated in MongoDB, as described below:

<1> "$gt", "$gte", "$lt", "$lte", "$ne", "No special keywords", which correspond to one by one above, give a few examples.

<2> "No keywords", "$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 query is very complex, very egg pain, but it doesn't matter, MongoDB gave us a big move, it is $where, why say, because $where in the value

Is that we are very familiar with the very love of JS to help us Yimapingchuan.

Three: Update operation

Update operation is nothing more than two, the overall update and local update, the use of the occasion I believe we are also clear.

<1> Overall Update

I do not know you can remember, I in the last article when using the update, in fact, that update is the overall update.

<2> Local Updates

Sometimes we just need to update a field instead of the whole update, so what do we do? Easy question, MongoDB has provided us with two of

Modifiers: $inc and $set.

① $inc modifier

$inc is the abbreviation of increase, the students who have learned SQL Server should be familiar with, for example, we do an online user status record, each modification will be on the original basis

Self-increment $inc The specified value, if the "document" does not have this key, then the key will be created, the following example can be understood.

② $set modifier

Don't say anything, just the code.

<3> Upsert operation

This is the "word" created by MongoDB, you remember the first parameter of the Update method is "query condition"? , then this upsert operation is saying: if I

Did not find, I was in the database add a, in fact, this also has the benefit, is to avoid me in the database inside the judge is update or add operation, use is very simple

Set the third parameter of update to TRUE.

<4> Batch Update

In MongoDB if you match more than one, the default is to update only the first one, then if we have a need to batch update, then the implementation in MongoDB is also very simple

, set to True in the fourth parameter of the update. The example is not to be lifted.

Four: Remove operation

This operation in the previous article simply said, here will not repeat.

MongoDB Learning Summary-2 (detail and additions to the search and change)

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.