[MongoDB] mongodb and php, mongodbphp_PHP tutorial

WBOY
Release: 2016-07-12 08:54:01
Original
911 people have browsed it

[MongoDB] mongodb and php, mongodbphp

Install mongodb php extension on windows

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

Find the dll file of the corresponding php version, download php_mongo.dll, put it in the ext directory under the php installation directory, modify php.ini, add an extension=php_mongo.dll, no dll supporting php7 was found

Get the MongoClient object and new it out

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

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

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

Call the find() method of the collection object to 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 to insert data, $collection->insert($data);

<?<span>php
</span><span>//</span><span> 连接到mongodb</span>
<span>$mongoClient</span> = <span>new</span><span> MongoClient();
</span><span>//</span><span> 选择一个数据库</span>
<span>$db</span> = <span>$mongoClient</span>-><span>test;

</span><span>//</span><span>获取集合</span>
<span>$collection</span>=<span>$db</span>-><span>users;

</span><span>//</span><span>更新文档</span>
<span>$condition</span>=<span>array</span><span>();
</span><span>$condition</span>["id"]=1<span>;
</span><span>$data</span>=<span>array</span><span>();
</span><span>$data</span>['name']="wangwu"<span>;
</span><span>$data</span>['age']="11"<span>;
</span><span>$collection</span>->update(<span>$condition</span>,<span>$data</span><span>);

</span><span>//</span><span>插入文档</span>
<span>$data</span>=<span>array</span><span>();
</span><span>$data</span>['id']=4<span>;
</span><span>$data</span>['name']="哈哈"<span>;
</span><span>$data</span>['age']="11"<span>;
</span><span>$collection</span>->insert(<span>$data</span><span>);

</span><span>//</span><span>删除文档</span>
<span>$condition</span>=<span>array</span><span>();
</span><span>$condition</span>['id']=2<span>;
</span><span>$collection</span>->remove(<span>$condition</span><span>);

</span><span>//</span><span>查询文档</span>
<span>$users</span>=<span>$collection</span>-><span>find();
</span><span>foreach</span> (<span>$users</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) {
    </span><span>print_r</span>(<span>$v</span><span>);
}
</span>?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1122396.htmlTechArticle[MongoDB] mongodb and php, mongodbphp Install the php extension download address of mongodb on windows https://s3.amazonaws .com/drivers.mongodb.org/php/index.html Find the dll file of the corresponding php version,...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template