Mongodb (initial)

Source: Internet
Author: User
Tags findone

First, I would like to thank cnblogs TV, the blogger TV, and friends who shared their support for the previous period. With your support, I was motivated to write down this issue. You may watch TV on the TV at night, or watch movies on the Internet. After sticking to my computer for a day, I continued to sit at the computer side and listened to 87.6 of Beijing Broadcasting Network, which said technology sharing. Now, the point is very important. Let's talk about our mongodb.

Mongodb can also provide almost all functions that mysql and sqlserver can do.

I. Operators

Operators are certainly known to all. They are equal to, greater than, less than, not equal to, greater than or equal to, and less than or equal to. However, they cannot be used directly in mongodb. The operators in mongodb are represented as follows:

(1) $ gt> (greater)

(2) $ lt <(less)

(3) $ gte >=( greater than or equal)

(4) $ lt <= (less than or equal)

(5) $ ne! = (Not equal)

(6) $ in (inclusive)

(7) $ nin not in (not included)

(8) $ exists exist (whether the field exists)

(9) $ inc adds value to a numeric field

(10) $ set is equivalent to SQL's set field = value.

(11) $ unset indicates deleting a field.

(12) $ push append the value to the field. The field must be of the array type. If the field does not exist, a new array type will be added.

(13) $ pushAll is the same as $ push, but multiple values can be appended to an array field at a time.

(14) $ addToSet adds a value to the array and increases only when the value is not in the array.

(15) $ pop Delete the last value: {$ pop: {field: 1} Delete the first value: {$ pop: {field:-1}. Note, only one value can be deleted, that is, only 1 or-1 can be used, and 2 or-2 cannot be used to delete two values. Mongodb 1.1 and later versions are available

(16) $ pull deletes a value equal to the value from the array field

(17) $ pullAll is the same as $ pull. Multiple values in the array can be deleted at a time.

(18) The $ operator is his own meaning, which indicates a certain item in the array identified by conditions. This is not enough.

Ii. Adding, modifying, reading, and deleting CURD

Add

 

db.collection->insert({'name' => 'caleng', 'email' => 'admin#admin.com'});

 

Isn't it easy? It's so simple. It has no field restrictions. you can name it and insert data at will.

Modify

 

Db. collection. update ({"count": {$ gt: 1 }},{ $ set: {"test2": "OK"}); only the first record db greater than 1 is updated. collection. update ({"count": {$ gt: 3 }},{ $ set: {"test2": "OK" }}, false, true ); all records greater than 3 have updated the database. collection. update ({"count": {$ gt: 4 }},{ $ set: {"test5": "OK" }}, true, false ); only the first db is added for records greater than 4. collection. update ({"count": {$ gt: 5 }},{ $ set: {"test5": "OK" }}, true, true ); more than 5 records are added

Query

db.collection.find(array('name' => 'bailing'), array('email'=>'email@qq.com'))db.collection.findOne(array('name' => 'bailing'), array('email''email@qq.com'))

You can see that I have used two different ways to query the logs. Why? In fact, this is the same as cooking. different spices are used and the fried dishes have different tastes. The following describes the different effects of the two spices.

FindOne () returns only one document object, and find () returns a list of sets.

That is to say, if we only want to query the details of a specific data, we can use findOne ();

If you want to query a group of information, such as a news list, we can use find ();

So I think everyone will think that I want to sort this list. no problem mongodb will serve you wholeheartedly.

Db. collection. find (). sort ({age: 1}); // sort the db in positive order by age. collection. find (). sort ({age:-1}); // sort the db in descending order by age. collection. count (); // obtain the total number of db data. collection. limit (1); // the start position of the database to fetch data. collection. skip (10); // obtain the end position of the Data. // This allows us to retrieve and sort 10 pieces of data.

Delete

Delete has two operations: remove () and drop ()

Db. collection. remove ({"name", 'Jerry '}) // Delete the specified data db. collection. drop () // delete all data in the Set

Distinct operation

db.user.distinct('name', {'age': {$lt : 20}})

 

 

 

Oh! There are too many writes in one breath, and it is hard for everyone to read too much. Let's get here today, and write php operations on mongodb tomorrow. Please look forward to it! You can't write any more. Otherwise, it will become a panda tomorrow. Good night. have a good dream.

 

Author 5: sarah

 

 

 

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.