摘要:本文主要向大家介绍了Mongodb API使用,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
本文主要向大家介绍了Mongodb API使用,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
编译安装PHP7 Mongdb扩展
[plain] view plain copy print?
1. #先安装一个依赖库
2. yum -y install openldap-devel
3. wget https://pecl.php.net/get/mongodb-1.1.1.tgz
4. /home/server/php7/bin/phpize #根据自己编译的PHP环境而定
5. ./configure --with-php-config=/home/server/php7/bin/php-config
6. make && make install
7.
8. #如果成功,生成一个mongodb.so扩展在lib/php/extensions/no-debug-non-zts-20151012/
9. 修改php.ini配置
10. extension=mongodb.so
注:
以前版本用的是mongo.so扩展,老的php-mongodb api
在PHP7已经不支持了,至少目前不支持。
最新支持PHP7的mongodb 编译后 仅支持新版API(mongodb > 2.6.X版本)
参考资料
GITHUB:
https://github.com/mongodb/
官网:
//www.mongodb.org/
PHP官方:
https://pecl.php.net/package/mongodb
//pecl.php.net/package/mongo [已废弃,目前只支持到PHP5.9999]
API手册:
//docs.php.net/manual/en/set.mongodb.php
Mongodb API 操作
初始化Mongodb连接
[plain] view plain copy print?
1. $manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
2. var_dump($manager);
[html] view plain copy print?
1. object(MongoDB\Driver\Manager)#1 (3) {
2. ["request_id"]=>
3. int(1714636915)
4. ["uri"]=>
5. string(25) "mongodb://localhost:27017"
6. ["cluster"]=>
7. array(13) {
8. ["mode"]=>
9. string(6) "direct"
10. ["state"]=>
11. string(4) "born"
12. ["request_id"]=>
13. int(0)
14. ["sockettimeoutms"]=>
15. int(300000)
16. ["last_reconnect"]=>
17. int(0)
18. ["uri"]=>
19. string(25) "mongodb://localhost:27017"
20. ["requires_auth"]=>
21. int(0)
22. ["nodes"]=>
23. array(...)
24. ["max_bson_size"]=>
25. int(16777216)
26. ["max_msg_size"]=>
27. int(50331648)
28. ["sec_latency_ms"]=>
29. int(15)
30. ["peers"]=>
31. array(0) {
32. }
33. ["replSet"]=>
34. NULL
35. }
36. }
CURL操作
[php] view plain copy print?
1. $bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
2. $bulk->delete([]);
3. $bulk->insert(['_id' => 1]);
4. $bulk->insert(['_id' => 2]);
5. $bulk->insert(['_id' => 3, 'hello' => 'world']);
6. $bulk->update(['_id' => 3], ['$set' => ['hello' => 'earth']]);
7. $bulk->insert(['_id' => 4, 'hello' => 'pluto']);
8. $bulk->update(['_id' => 4], ['$set' => ['hello' => 'moon']]);
9. $bulk->insert(['_id' => 3]);
10. $bulk->insert(['_id' => 4]);
11. $bulk->insert(['_id' => 5]);
12.
13.
14. $manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
15. $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
16.
17.
18. try {
19. $result = $manager->executeBulkWrite('db.collection', $bulk, $writeConcern);
20. } catch (MongoDB\Driver\Exception\BulkWriteException $e) {
21. $result = $e->getWriteResult();
22.
23.
24. // Check if the write concern could not be fulfilled
25. if ($writeConcernError = $result->getWriteConcernError()) {
26. printf("%s (%d): %s\n",
27. $writeConcernError->getMessage(),
28. $writeConcernError->getCode(),
29. var_export($writeConcernError->getInfo(), true)
30. );
31. }
32.
33.
34. // Check if any write operations did not complete at all
35. foreach ($result->getWriteErrors() as $writeError) {
36. printf("Operation#%d: %s (%d)\n",
37. $writeError->getIndex(),
38. $writeError->getMessage(),
39. $writeError->getCode()
40. );
41. }
42. } catch (MongoDB\Driver\Exception\Exception $e) {
43. printf("Other error: %s\n", $e->getMessage());
44. exit;
45. }
46.
47.
48. printf("Inserted %d document(s)\n", $result->getInsertedCount());
49. printf("Updated %d document(s)\n", $result->getModifiedCount());
查询
[php] view plain copy print?
1. $filter = array();
2. $options = array(
3. /* Only return the following fields in the matching documents */
4. "projection" => array(
5. "title" => 1,
6. "article" => 1,
7. ),
8. "sort" => array(
9. "views" => -1,
10. ),
11. "modifiers" => array(
12. '$comment' => "This is a query comment",
13. '$maxTimeMS' => 100,
14. ),
15. );
16.
17. $query = new MongoDB\Driver\Query($filter, $options);
18.
19. $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
20. $readPreference = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
21. $cursor = $manager->executeQuery("databaseName.collectionName", $query, $readPreference);
22.
23. foreach($cursor as $document) {
24. var_dump($document);
25. }
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号