In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about how to operate the MongoDB database in PHP. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.
1. Connect MongoDB database $conn=new Mongo (); other link methods / / $conn=new Mongo (); # connect local host, default port. / / $conn=new Mongo ("172.21.15.69"); # connect remote host / / $conn=new Mongo ("xiaocai.loc:10086"); # connect designated port remote host / / $conn=new Mongo ("xiaocai.loc", array ("replicaSet" = > true)) # load balancer / / $conn=new Mongo ("xiaocai.loc", array ("persist" = > "t")); # persistent connection / / $conn=new Mongo ("mongodb://sa:123@localhost"); # with username password / / $conn=new Mongo ("mongodb://localhost:27017,localhost:27018"); # connect multiple servers / / $conn=new Mongo ("mongodb:///tmp/mongo-27017.sock") # Domain socket / / $conn=new Mongo ("mongodb://admin_miss:miss@localhost:27017/test", array ('persist'= >' paired, "replicaSet" = > true)); # complete 2, select database and table $db=$conn- > mydb; # Select mydb database $collection=$db- > myTable; # Select collection (select 'table') / / $collection=$db- > selectCollection (myTable); # second way / / $collection=$conn- > mydb- > myTable # more concise writing 3, insert data record $array=array ('column_name'= >' col'.rand (100999), 'column_exp'= >' xiaocai'); $result=$collection- > insert ($array); # simply insert echo "new record ID:". $array ['_ id']; # MongoDB will return a record ID var_dump ($result); # return: bool (true) / / * * safely insert data into the collection and return the insertion status (array). * / $array=array ('column_name'= >' col'.rand (100999), 'column_exp'= >' xiaocai2'); $result=$collection- > insert ($array,true); # is used to wait for the MongoDB to complete the operation to determine whether it is successful. (this parameter is useful when a large number of records are inserted.) echo "new record ID:". $array ['_ id']; # MongoDB returns a record ID var_dump ($result) # return: array (3) {["err"] = > NULL ["n"] = > int (0) ["ok"] = > float (1)} 4, update data record / / * * modify update traditional update * * / $where=array ('column_name'= >' col123'); $newdata=array ('column_exp'= >' GGGGGGG','column_fid'= > 444); $result=$collection- > update ($where,array ('$set'= > $newdata)) # $set: make a node equal to a given value, and similarly, $pull $pullAll $pop $inc. The usage / * * result is explained later: * original data * {"_ id": ObjectId ("4d635ba2d549a02801000003"), "column_name": "col123", "column_exp": "xiaocai"} * replaced with * {"_ id": ObjectId ("4d635ba2d549a02801000003"), "column_name": "col123", "column_exp": "GGGGGGG" "column_fid": 444} * / / * * replace and update Overwrite the original record * * / $where=array ('column_name'= >' col709') $newdata=array ('column_exp'= >' HHHHHHHHH','column_fid'= > 123); $result=$collection- > update ($where,$newdata) / * * result: * original data * {"_ id": ObjectId ("4d635ba2d549a02801000003"), "column_name": "col709", "column_exp": "xiaocai"} * was replaced by * {"_ id": ObjectId ("4d635ba2d549a02801000003"), "column_exp": "HHHHHHHHH", "column_fid": 123} * / / * * batch update * * / $where=array ('column_name'= >' col') $newdata=array ('column_exp'= >' multiple','91u'= > 684435); $result=$collection- > update ($where,array ('$set'= > $newdata), array ('multiple'= > true)); / * * all' column_name'='col' are modified * / / * * automatically accumulate * / $where=array ('91ubike = > 684435); $newdata=array (' column_exp'= > 'edit'); $result=$collection- > update ($where,array (' $set'= > $newdata,'$inc'= > array ('91ubike = >-5) / * update 91u=684435 data, and delete record operation / / * * delete node * * / $where=array ('column_name'= >' col685'); $result=$collection- > update ($where,array ('$unset'= > 'column_exp')); / * * delete node column_exp*//** full format: update (array $criteria, array $newobj [, array $options = array ()]) * Note: 1. Pay attention to distinguish between replacement update and modified update * 2. Pay attention to distinguishing between data types such as array ('91uqi = >' 684435') and array ('91ubeauty = > 684435) * * / / * * clear table * * / $collection- > remove (); # empty collection / / * * delete specified MongoId * * / $id = new MongoId ("4d638ea1d549a02801000011"); $collection- > remove (' _ id'= > (object) $id) 6. Query data records / / * * statistics table records * * / echo 'count:'.$collection- > count (). "
"; # all echo 'count:'.$collection- > count (array (' type'= > 'user'))
"; # Statistics' records whose type' is user echo 'count:'.$collection- > count (' age'= > array ('$gt'= > 50 recording records = > 74))."
"; # Statistics greater than 50 less than or equal to 74echo 'count:'.$collection- > find ()-> limit (5)-> skip (0)-> count (true)."
"; # get the actual number of returned results / * * Note: $gt is greater than, $gte is greater than or equal to, $lt is less than, $lte is less than or equal to, $ne is not equal, $exists does not exist * / / $cursor = $collection- > find ()-> snapshot (); foreach ($cursor as $id = > $value) {echo" $id: "; var_dump ($value); echo"
" } / * Note: * after we do the find () operation and get the $cursor cursor The cursor is still dynamic. * in other words, after I find (), until my cursor loop is completed, if any more eligible records are inserted into collection, then these records will also be obtained by $cursor. * if you want the result set to remain unchanged after getting $cursor, you need to do this: * $cursor = $collection- > find () * / / * * query a piece of data * * / $cursor = $collection- > findOne (); / * Note: findOne () cannot use functions such as snapshot () and fields () after obtaining the result set; * / / * * set the display field age,type column not to display * * / $cursor = $collection- > find ()-> fields ("age" = > false, "type" = > false); $cursor = $collection- > find ()-> fields (array ("user" = > true)) / / display only the user column / * I will make an error if I write it this way: $cursor- > fields ("age" = > true, "type" = > false); * / / * * set the condition (there is a type,age node) and exists'= > true 0 and agearray ('$exists'= > true), 'age'= > array (' $ne'= > 0 minicomination lttle = > 50 pencils records = > true); $cursor = $collection- > find ($where) / / * * get the result set by paging * * / $cursor = $collection- > find ()-> limit (5)-> skip (0); / / * * sort * * / $cursor = $collection- > find ()-> sort ('age'= >-1 graded category = > 1); # # 1 means descending-1 indicates ascending order, and the order of parameters affects the sort order / / * * index * / $collection- > ensureIndex (array (' age'= > 1th category = >-1)) # 1 indicates descending order-1 means ascending order $collection- > ensureIndex (array ('age' = > 1 recording typewriter = >-1), array (' background'= > true)); # Index creation is run in the background (default is synchronous) $collection- > ensureIndex ('age' = > 1 camera typewriter = >-1), array (' unique'= > true)) # the index is unique / * ensureIndex (array (), array ('name'= >' index name', 'background'=true,'unique'=true)) * for more information, please see: http://www.php.net/manual/en/mongocollection.ensureindex.php*///** gets query results * * / $cursor = $collection- > find (); $array=array (); foreach ($cursor as $id = > $value) {$array [] = $value;} 7. Close the link $conn- > close (); # close connection 8, common functions use
A.$inc if the recorded node exists, let the value of the node add N; if the node does not exist, let the value of the node equal to N
Set the structure record structure to array ('a record = > 1 recording breadth = >'t'). If you want to add a to 5, then:
$coll- > update (array ('bounded = >' t'), array ('$inc'= > array ('averse = > 5)
B.$set makes a node equal to a given value
Let the structure record structure be array ('a record = > 1 recording breadth = >'t'), b is plus f, then:
$coll- > update (array ('averse = > 1), array (' $set'= > array ('baked = >' f')
C.$unset deletes a node
If you want to delete the b node if you want to delete the b node, set the record structure to array ('a record = > 1 recording breadth = >'t').
$coll- > update (array ('averse = > 1), array (' $unset'= >'b'))
D.$push if the corresponding node is an array, append a new value to it; if it does not exist, create the array and append a value to the array; if the node is not an array, an error is returned.
Set the record structure to array ('a record = > array (0 = > ''),' bounded data & gt;1). If you want to attach new data to node a, then:
$coll- > update (array ('bounded = > 1), array (' $push'= > array ('wow' = >' wow')
In this way, the record becomes: array ('',1= = > array (0 = >' ',1= > 'wow'),' bounded = > 1)
E.$pushAll is similar to $push, except that multiple values are appended to a node at a time
F.$addToSet if there is no value in the array at this stage, add it
Set the record structure to array ('a record = > array (0 & gt;''), 'breadth = > 1). If you want to attach new data to this node a, then:
$coll- > update (array ('bounded = > 1), array (' $addToSet'= > array ('wow' = >' wow')
If you already have a wow in node a, no new one will be added, and if not, a new item--wow will be added for that node.
G.$pop sets the record to array ('',1= = > array (0 = >' ',1= > 'wow'),' bounded = > 1)
Delete the last element of an array node:
$coll- > update (array ('bounded = > 1), array (' $pop= > array ('averse = > 1)
Delete the first element of an array phase
$coll- > update (array ('bounded = > 1), array (' $pop= > array ('averse = >-1)
H.$pull if the node is an array, delete the child whose value is value, and if it is not an array, an error will be returned.
Set the record to array ('a record = > array (0 = > '',1= >' wow'), 'bounded = > 1), and you want to delete the subentry in a whose value is :
$coll- > update (array ('bounded = > 1), array (' $pull= > array ('' = >' ')
The result is: array ('averse = > array (0 = >' wow'), 'bounded = > 1)
I.$pullAll is similar to $pull, except that you can delete a set of eligible records.
The above is how to operate the MongoDB database in the PHP shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.