1024programmer Mongodb Summary of common operations on MongoDB data in PHP

Summary of common operations on MongoDB data in PHP

This article mainly summarizes and introduces the common operations of MongoDB in PHP in detail. Friends who need it can come and refer to it. I hope it will be helpful to everyone.

$mongodb = new Mongo();

//$cOnnection= new Mongo( “$dburl:$port” ); // connect to a
remote host (default port)

$mydb = $mongodb->mydb; // implicitly create database mydb

$mydb = $mongodb->selectDB(“mydb”); //Select the existing database directly

$collection = $mydb->mycollect; //Select the collection used, if it does not exist, it will be created automatically

$collection = $db->selectCollection(‘mydb’); // only select, not create

//insert a new record

$collection->insert(array(“name”=>”l4yn3”,
“age”=>”10”, “sex”:”unknown”));

//Modify record

$where = array(“name”=>”l4yn3”);

$update_item = array(‘$set’=>array(“age”=>”15”,
“sex”:”secret”));

$collection->update($where, $update_item);

$options[‘multiple’] = true; //The default is false, whether to change the matching multiple lines

$collection->update($where, $update_item, $options);

//Query the records

$myinfo =
$collection->findOne(array(“name”=>”l4yn3”));

$myinfo = $collection->findOne(array(“name”=>

“l4yn3”), array(“age”=>”15”));

//Search by condition:

$query = array(“name”=>”l4yn3”);

$cursor = $collection->find($query);
//Find documents satisfying $query in the $collectio collection

while($cursor->hasNext())

{

var_dump($cursor->getNext()); // returns the array

}

//Return the number of document records

$collection->count();

//Delete a database:

$connection->dropDB(“…”);

//List all available databases:

$m->listDBs(); //No return value

//Close the connection:

$connection->close();

Various parameter methods for connecting to the mongodb database in php

//Connect to localhost:27017

$cOnn = new Mongo();

//Connect to the default port of the remote host

$cOnn = new Mongo(‘test.com’);

//Connect to port 22011 of the remote host

$cOnn= new Mongo(‘test.com:22011’);

//MongoDB has username and password

$cOnn = new
Mongo(“mongodb://${username}:${password}@localhost”)

//MongoDB has a username and password and specifies the database blog

$cOnn = new
Mongo(“mongodb://${username}:${password}@localhost/blog”);

//Multiple servers

$cOnn = new
Mongo(“mongodb://localhost:27017,localhost:27018”);

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/summary-of-common-operations-on-mongodb-data-in-php/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索