MongoDB Basic operation (add-and-revise check)

Source: Internet
Author: User
Tags bulk insert modifier

Basic operations

basic "additions and deletions", in the DOS environment input MONGO command to open the shell, in fact, this shell is the client of MongoDB , but also a JS compiler , the default connection is "test" Database .   "Error"first, when we open MongoDB in the same way, we can not turn on, carefully observe the "underlined area" information, found that the DB folder has a similar "lock file" to prevent the opening of MongoDB, the next thing we want to do is to kill it, and then open the success.

<1> Insert Operations

database has, the next step is the collection, here to take the set named "Person", to note that The document is an extended (Bson) Form of JSON.

Note: The writing format for the outside is (), the inside of the book {}.

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 may be "string", may be "array", and possibly an embedded JSON object, The same approach is also suitable 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.

   <2> Find Operations

After we insert the data, it is definitely to find out, or plug in the white plug, here to pay attention to two points:

① "_id": This field is the GUID that the database gives us by default, the purpose is to guarantee the uniqueness of the data.

② strictly in accordance with the Bson form of the document, but it doesn't matter, the error is very powerful.

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 the query is very complex, very egg pain, but it doesn't matter, MongoDB gave us a big recruit, it is $where, why so say, is because the value of $where is we are very familiar with, very love JS to help us Yimapingchuan.

<3> Update operation

the first parameter of the Update method is criteria for lookup, and the second argument is the updated value.   

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 modifiers: $inc and $set.

① $inc modifier

$inc is the abbreviation of increase, the students who have learned SQL Server should be very familiar with, for example, we do an online user status record, each modification will be on the basis of the original $inc the specified value, if the "document" does not have this key, will create a key, the following example can be understood.

 ② $set modifier 

<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 to say: If I did not find, I am in the database add a, in fact, this also has the advantage, 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, in the fourth parameter of the update is set to True. The example is not to be lifted.

 

<4> Remove operation

Remove if no parameters will delete all data, very dangerous operation, in MongoDB is a non-revocable operation, think twice before the line.

 

MongoDB Basic operation (add-and-revise check)

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.