PHP operation mongodb add, delete, modify and check

//php link mongodb

$m = new Mongo("mongodb://username:password@host:port}");

//Select the database, if the database does not exist before, the dbname will be automatically created

$m = $m->dbname;    //或者$m->selectDB(dbname)

//Select the table for operation, if there is no previous data table, table_name will be automatically created

$collection = $db->table_name;

//Insert data array: Insert data, must be an array 

$collection->insert(array); //return bool value

//Query data $where optional parameter, query condition, must be an array

$data = $collection->find($where) ; // $data returns an object, traversed using foreach

// sort data after query

$data->sort(array("field"=>-1)); //$data: query result, field: sort field-1: flashback 1: positive order traversal using foreach

//Delete data  array: delete condition, all data must be deleted if the array condition is empty

$collection->remove(array);  //返回bool

//Modify a piece of data $where: modify the condition, must be an array $array: modify the content, must be an array

$collection->update($where,$array)  //返回bool

//Modify a demo with _id as the condition to modify, the modification condition must be written like this: array("_id"=>new MongoId('5aa886bb358cabb81800002a'))

//Modify a demo to modify other fields as conditions, the modification conditions must be written as follows: array("field"=>'value')

$array = ['$set'=>["field"=>"Modify value","field"=>"Modify value"]];

$collection->update(array("_id"=>new MongoId('5aa886bb358cabb81800002a')),$array); //返回bool

//Modify multiple pieces of data $where: modify the condition, must be an array $array: modify the content, must be an array

$collection->update($where,$array,array("multiple" => true))  //返回bool

//Modify multiple demos

$array = ['$set'=>["field"=>"Modify value","field"=>"Modify value"]];

$collection->update(array("filed"=>'值'),$array); //返回bool

// query a single 

//$query: query condition Query with _id as condition, the condition must be written like this: array("_id"=>new MongoId('5aa886bb358cabb81800002a'))

//$query: query condition other conditional query, the condition is written like this: array("field"=>'value'))

$data = $collection->findOne($query); //return a one-dimensional array


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325908217&siteId=291194637