mongodb+php implementation of simple addition deletion change and check

  • 2020-05-30 21:14:45
  • OfStack

Install the php extension of mongodb on windows

Download address https: / / s3. amazonaws. com/drivers mongodb. org/php/index html

Find the corresponding php version of dll file and download it php_mongo.dll , put it in the ext directory under the php installation directory, modify php.ini, and add 1 item extension=php_mongo.dll , dll support for php7 was not found

Get the MongoClient object, new out Gets the database object db, via the database properties of the MongoClient object, $MongoClient- > The database name Gets the collection collection, via the collection properties of the db object, $db- > A collection of Create the collection, call the createCollection() method of the db object, Call the find() method of the collection object, query the data, $collection- > find() Call the update () method of the collection object to update the data, $collection- > update($condition,$data); Call the insert () method of the collection object, insert the data, $collection- > insert($data);

<?php
//  Connect to the mongodb
$mongoClient = new MongoClient();
//  choose 1 A database 
$db = $mongoClient->test;

// For collection 
$collection=$db->users;

// Update the document 
$condition=array();
$condition["id"]=1;
$data=array();
$data['name']="wangwu";
$data['age']="11";
$collection->update($condition,$data);

// Inserted into the document 
$data=array();
$data['id']=4;
$data['name']=" Ha ha ";
$data['age']="11";
$collection->insert($data);

// Delete the document 
$condition=array();
$condition['id']=2;
$collection->remove($condition);

// Query the document 
$users=$collection->find();
foreach ($users as $k => $v) {
  print_r($v);
}
?>

Related articles: