Home  >  Article  >  Backend Development  >  PHP extension to mongodb (fledgling)_PHP tutorial

PHP extension to mongodb (fledgling)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:14:22806browse

Our php mongodb can also do almost all the functions that mysql and sqlserver can do. This article will introduce in detail

1. Operators
I believe everyone must know it, that is Equal to, greater than, less than, not equal to, greater than or equal to, less than or equal to, but these operators cannot be used directly in mongodb. The operator in mongodb is expressed like this:
(1) $gt > (greater than) 
(2) $lt Equal to)
(4) $lt  <= (less than or equal to) 
(5) $ne != (not equal to) 
(6) $in in (inclusive)  nin not in (not included) 
(8) $exists exist (whether the field exists) 
(9) $inc Add value to a numeric field field
(10) $set It is equivalent to the set of sql field = value
(11) $unset It is to delete the field
(12) $push Append the value to the field. The field must be an array type. If the field does not exist, a new array type will be added. Go in
(13) $pushAll Same as $push, except that multiple values ​​can be appended to an array field at one time
(14) $addToSet Adds a value to the array, and only adds it if the value is not in the array .
(15) $pop Delete the last value: { $pop : { field : 1 } } Delete the first value: { $pop : { field : -1 } } Note that only one value can be deleted, that is It says that you can only use 1 or -1, but not 2 or -2 to delete two items. Only mongodb 1.1 and later versions can use
(16) $pull to delete a value equal to value from the array field
(17) $pullAll Same as $pull, you can delete multiple values ​​in the array at one time
(18) The $ operator is his own meaning, representing himself to find an item in the array based on conditions. This one is more difficult, so I won’t talk about it.


2. CURD Add, modify, read, delete
Add

Copy code The code is as follows:
db.collection->insert({'name' => 'caleng', 'email' => 'admin#admin.com'});



Isn’t it very simple? Yes, it is that simple. It has no field restrictions. You can name it as you like and insert data


Modify

Copy code The 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 are updated
db.collection.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK" } },true,false ); Records greater than 4 are only added to the first
db.collection.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true ); Add all records greater than 5


Query

Copy code The 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 In the query, I used two different ways of writing it. This is why. In fact, it is the same as cooking. Adding different seasonings will result in different flavors in the dishes. Let me tell you the different functions of these two seasonings.
findOne() only returns a document object, and find() returns a collection list.
That is to say, for example, if we only want to check the detailed information of a specific piece of data, we can use findOne();
If we want to query a certain set of information, such as a news list, we can It can be used as find();
Then I think everyone will think that I want to sort this list. No problem mongodb will serve you wholeheartedly


Copy code The code is as follows:
db.collection.find().sort({age:1}); //Arrange in positive order according to age
db.collection.find( ).sort({age:-1}); //Arrange in reverse order by age
db.collection.count(); //Get the total number of data
db.collection.limit(1); //Get data The starting position of
db.collection.skip(10); //Get the end position of the data
//In this way, we have implemented an operation of taking 10 pieces of data and sorting them.


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

Copy code The code is as follows:
db.collection.remove({"name",'jerry'}) //Delete specific data
db.collection.drop() //Delete the data in the collection All data


distinct operation
Copy code The code is as follows:

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

Oh! It’s too much to write in one breath, and it’s hard for everyone to digest after reading too much. That’s it for today. Tomorrow I will write about the operation of php on mongodb. Please look forward to it! I can’t write anymore, otherwise I will turn into a panda tomorrow. good night. have a good dream.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326319.htmlTechArticleOur php mongodb can also do almost all the functions that mysql and sqlserver can do. This article will introduce in detail 1. Operation I believe everyone must know the operators, which are equal to, greater than, less...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn