Home > php教程 > PHP开发 > Mongodb+php implements simple addition, deletion, modification and query

Mongodb+php implements simple addition, deletion, modification and query

高洛峰
Release: 2016-12-23 09:16:52
Original
1311 people have browsed it

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 the dll file, download php_mongo.dll, and put Go to the ext directory under the php installation directory, modify php.ini, add an extension=php_mongo.dll, no dll supporting php7 is found

Get the MongoClient object, new comes out

Get the database object db, through the database of the MongoClient object Attribute, $MongoClient->database name

Get the collection, through the collection attribute of the db object, $db->collection name

Create a collection, call the createCollection() method of the db object,

call the find of the collection object () method, query data, $collection->find()

Call the update () method of the collection object, update the data, $collection->update($condition,$data);

Call the insert of the collection object () method, insert data, $collection->insert($data);

<?php
// 连接到mongodb
$mongoClient = new MongoClient();
// 选择一个数据库
$db = $mongoClient->test;
 
//获取集合
$collection=$db->users;
 
//更新文档
$condition=array();
$condition["id"]=1;
$data=array();
$data[&#39;name&#39;]="wangwu";
$data[&#39;age&#39;]="11";
$collection->update($condition,$data);
 
//插入文档
$data=array();
$data[&#39;id&#39;]=4;
$data[&#39;name&#39;]="哈哈";
$data[&#39;age&#39;]="11";
$collection->insert($data);
 
//删除文档
$condition=array();
$condition[&#39;id&#39;]=2;
$collection->remove($condition);
 
//查询文档
$users=$collection->find();
foreach ($users as $k => $v) {
  print_r($v);
}
?>\
Copy after login

For more mongodb+php to implement simple addition, deletion, modification and query related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template