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 knows the operators, which are 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 operators in mongodb are expressed as follows:
(1) $gt > (greater than)
(2) $lt < (less than)
(3) $gte >= (greater than or equal to)
(4) $lt (Whether the field exists)
(9) $inc Append to the field. The field must be an array type. If the field does not exist, a new array type will be added.
(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 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 that only one value can be deleted, that is, only You can use 1 or -1, but not 2 or -2 to delete two items. Mongodb 1.1 and later versions can only 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) $ operator It's his own meaning, and represents himself as an item in the array found 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 have been updated
db.collection.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false ); Records greater than 4 are only added to the first one
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 that I used two different ways of writing the query. This is why. In fact, it is the same as cooking. Different seasonings are added and stir-fried. The dishes are different in taste. 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 use find() );
Then I think everyone will think that I want to sort this list, no problem mongodb will serve you wholeheartedly
Copy the code The code is as follows:
db.collection.find().sort({age:1 }); //Arrange in positive order by 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 the starting position of the data
db.collection.skip(10); //Get the ending position of the data
//In this way we have implemented a method of taking 10 pieces of data and sorting them operate.
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 all data in the collection
distinct operation
Copy code The code is as follows:
db.user.distinct('name', {'age': {$lt : 20}})