mongodb://[username:[email protected]]host1[:p ort1][,host2[:p Ort2],... [, hostn[:p Ortn]] [/[database][?options]]
- mongodb:// This is a fixed format that must be specified.
- username:[email protected] option, if set, after connecting to the database server, the driver will attempt to log into this database
- host1 must specify at least one host, host1 is the only thing to fill in this URI. It specifies the address to connect to the server. If you are connecting to a replica set, specify multiple host addresses.
- portx optional specified port, If not, the default is 27017
- /database If specify username:[email protected], connect and verify login to the specified database. If not specified, the admin database is opened by default.
- ?options is a connection option. If you do not use/database, you need to add/. All connection options are key-value pairs Name=value, and key-value pairs are separated by & or; (semicolon)
$m = new Mongoclient ("Mongodb://root:[email protected]:27017");//Connect MongoDB//colon preceded by a user name Colon followed by the first part is the password then the host or IP port /c0>//$db = $m->test;//Get a database with the name "test"$db = $m->selectdb ("test");$creatCollect = $db->createcollection ("Zhang");//$collection = $db->selectcollection ("Runoob");//Select Collection$collection = $db->selectcollection ("Zhang"); $data = Array (" title" = "MongoDB","url" = "http://www.runoob.com/mongodb/",' Log_time ' =>date ("y-m-d h:i:s"),);$data 1=array (' title ' = ' MongoDB ');//$collection->remove ($data 1);//delete operation$collection->remove ($data 1,array ("justone" = True));//delete only one record//$collection->insert ($data);//insert Operation//Updating Data operation Update (DATA,NEWOBJ,OPT)$sarr = Array ("title" + "MongoDB");//library in the original data query queries$darr = Array (' $set ' =>array (' title ' = ' Zhangsan '));//data to be modified$opts = Array (' Upsert ' =>0, ' multiple ' =>1);$collection->update (' title ' + ' Zhang '), Array (' $set ' = + $data 1), $opts);//Find Data$collection->find ($data 1);//$collection->findone ($data 1)//Search by criteria and limit the number of bars that display the data$query =array (' Log_time ' =>array (' $gt ' = ' 2017-02-22 '));$result = $collection->find ($query)->limit (3);//sort () sort (Array (' key ' =>-1)) 1 is ascending-1 is in reverse order default to ascending key refers to field$result = $collection->find ($query)->limit (3)->sort (Array (' Log_time ' =>-1));
PHP operations for CRUD in MongoDB