相关链接
Mongodb php driver
安装驱动包以及工具包$ pecl install mongodb
$ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
$ composer require "mongodb/mongodb=^1.0.0"
获取云数据库MongoDB的连接信息
请查看
MongoDB 连接信息七要素获取方法。
PHP Demo Code<?php
require 'vendor/autoload.php'; // include Composer goodies
# 实例信息
$demo_seed1 = 'demotest-1.mongodb.test.aliyun-inc.com:3717';
$demo_seed2 = 'demotest-2.mongodb.test.aliyun-inc.com:3717';
$demo_replname = "mgset-1441984463";
$demo_user = 'root';
$demo_password = '123456';
$demo_db = 'admin';
# 根据实例信息构造mongodb connection string
# mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
$demo_uri = 'mongodb://' . $demo_user . ':' . $demo_password . '@' .
$demo_seed1 . ',' . $demo_seed2 . '/' . $demo_db . '?replicaSet=' . $demo_replname;
$client = new MongoDB\Client($demo_uri);
$collection = $client->testDb->testColl;
$result = $collection->insertOne( [ 'name' => 'ApsaraDB for Mongodb', 'desc' => 'Hello, Mongodb' ] );
echo "Inserted with Object ID '{$result->getInsertedId()}'", "\n";
$result = $collection->find( [ 'name' => 'ApsaraDB for Mongodb'] );
foreach ($result as $entry)
{
echo $entry->_id, ': ', $entry->name, "\n";
}
?>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。