Our PHP MongoDB can do almost all the functions that MySQL and SQL Server can do, this article will explain in detail
One, operator
Operators believe everyone must know, is equal to, greater than, less than, not equal to, greater than or equal to, less than equals, but in MongoDB can not directly use these operators. The operators in MongoDB are represented in this way:
(1) $gt > (greater than)
(2) $lt < (less than)
(3) $gte >= (greater than or equal)
(4) $lt <= (less than or equal)
(5) $ne! = (Not equal)
(6) $in in (included)
(7) $nin not in (not included)
(8) $exists exist (whether the field exists)
(9) Add Value to a Field field $inc to a number
(Ten) $set is the equivalent of SQL set field = value
(one) $unset is to delete a field
$push Append value To field, field must be an array type, and if field does not exist, a new array type will be added.
$pushAll with $push, it is possible to append multiple values to an array field at a time
$addToSet adds a value to the array, and only if the value is not in the array.
$pop Delete the last value: {$pop: {field:1}} deletes the first value: {$pop: {field:-1}} Note that only one value can be deleted, that is, only 1 or-1, not 2 or--and two. MongoDB 1.1 and later versions are only available
$pull Delete an equal value from the array field
$pullAll with $pull, you can delete multiple values in an array at once
(18) The $ operator is his own meaning, which represents a condition of finding an array inside of himself. This compares the mouth, will not say.
ii. curd Increase, change, read and delete
Increase
Copy CodeThe code is as follows:
Db.collection->insert ({' Name ' = ' Caleng ', ' email ' = ' admin#admin.com '});
is not gray often simple ah, yes is so simple, it has no field restrictions, you can arbitrarily name, and insert data
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); Records greater than 3 are all 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); Records greater than 5 are all added in.
Enquiry
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 the query I used two different ways of writing, which is why, in fact, this is the same as cooking is the same, put different spices, fried vegetables are different flavor. Let's talk about the different effects of the two spices.
FindOne () returns only one document object, and find () returns a list of collections.
That is to say, we just want to check the details of a particular piece of data, we can use FindOne ();
If you want to query a certain set of information, such as a news list, we can function as find ();
Then I think you 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}); In chronological order
Db.collection.find (). Sort ({age:-1}); In reverse chronological order
Db.collection.count (); Get the total number of data
Db.collection.limit (1); Where to begin the data collection
Db.collection.skip (10); Where to end the data
This allows us to implement an operation that takes 10 data and sorts it.
Delete
Delete has two actions 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 collection
Distinct operation
Copy CodeThe code is as follows:
Db.user.distinct (' name ', {' age ': {$lt: 20}})
Oh! Write too much in one breath, we see too much also difficult to digest. Today will be here, tomorrow and then write the PHP operation of MongoDB, please look forward to Oh! You can't write any more, otherwise you'll change pandas tomorrow. Good night. Have a good dream.
http://www.bkjia.com/PHPjc/326319.html www.bkjia.com true http://www.bkjia.com/PHPjc/326319.html techarticle our PHP MongoDB can also do MySQL, SQL Server can do almost all the functions, this article will be introduced in detail one, operator operators believe you must know, is equal to, greater than, small ...