Php extension to mongodb (initial)

Source: Internet
Author: User
Tags findone php mongodb

Our php mongodb can also provide almost all functions that mysql and SQL server can do. This article will introduce in detail

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
Copy codeThe Code is as follows: 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 Copy codeThe Code is as follows: db. collection. update ({"count": {$ gt: 1 }},{ $ set: {"test2": "OK"}); only the first record greater than 1 is updated.
Db. collection. update ({"count": {$ gt: 3 }},{ $ set: {"test2": "OK" }}, false, true ); all records greater than 3 have been updated.
Db. collection. update ({"count": {$ gt: 4 }},{ $ set: {"test5": "OK" }}, true, false ); only the first record with a value greater than 4 is added.
Db. collection. update ({"count": {$ gt: 5 }},{ $ set: {"test5": "OK" }}, true, true ); more than 5 records are added

Query Copy codeThe Code is as follows: 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.Copy codeThe Code is as follows: db. collection. find (). sort ({age: 1}); // sort by age in the forward order
Db. collection. find (). sort ({age:-1}); // sort by age in reverse order
Db. collection. count (); // obtain the total number of data.
Db. collection. limit (1); // the starting position of the data.
Db. collection. skip (10); // The end position of the retrieved data.
// In this way, we implement a sorting operation for 10 pieces of data.

Delete
Delete has two operations: remove () and drop ()Copy codeThe Code is as follows: db. collection. remove ({"name", 'Jerry '}) // delete specific data
Db. collection. drop () // delete all data in the Set

Distinct operationCopy codeThe Code is as follows: 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.

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.