云数据库MongoDB版完全兼容MongoDB协议,本文介绍各类程序连接数据库的相关示例代码。

相关链接

Node.js 连接示例

相关链接:MongoDB Node.js Driver

  1. 项目初始化。
    mkdir node-mongodb-demo
    cd node-mongodb-demo
    npm init
  2. 安装驱动包以及工具包。
    npm install mongodb node-uuid sprintf-js –save
  3. 获取云数据库 MongoDB 连接信息。
  4. Node.js Demo Code。
        'use strict';
        var uuid = require('node-uuid');
        var sprintf = require("sprintf-js").sprintf;
        var mongoClient = require('mongodb').MongoClient;
        var host = "dds-t4n********.mongodb.singapore.rds.aliyuncs.com";
        var port = 3717;
        var username = "user********";
        var password = "123456";
        var demoDb = "test";
        var demoColl = "testColl";
        // 官方建议使用的方案
        var url = sprintf("mongodb://%s:%d/%s", host, port, demoDb);
        console.info("url:", url);
        // 获取mongoClient
        mongoClient.connect(url, function(err, db) {
            if(err) {
                console.error("connect err:", err);
                return 1;
            }
            // 授权. 这里的username基于admin数据库授权
            var adminDb = db.admin();
            adminDb.authenticate(username, password, function(err, result) {
                if(err) {
                    console.error("authenticate err:", err);
                    return 1;
                }
                // 取得Collecton句柄
                var collection = db.collection(demoColl);
                var demoName = "NODE:" + uuid.v1();
                var doc = {"DEMO": demoName, "MESG": "Hello AliCoudDB For MongoDB"};
                console.info("ready insert document: ", doc);
                // 插入数据
                collection.insertOne(doc, function(err, data) {
                    if(err) {
                        console.error("insert err:", err);
                        return 1;
                    }
                    console.info("insert result:", data["result"]);
                    // 读取数据
                    var filter = {"DEMO": demoName};
                    collection.find(filter).toArray(function(err, items) {
                        if(err) {
                            console.error("find err:", err);
                            return 1;
                        }
                        console.info("find document: ", items);
                        //关闭Client,释放资源
                        db.close();
                    });
                });
            });
        });

PHP 连接示例

相关链接:

Mongodb php driver

  1. 安装驱动包以及工具包。
        $ pecl install mongodb
        $ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
        $ composer require "mongodb/mongodb=^1.0.0"
  2. 获取云数据库MongoDB的连接信息。
  3. PHP Demo Code。
        <?php
        require 'vendor/autoload.php'; // include Composer goodies
            # 实例信息
            $demo_seed = 'dds-t4n********.mongodb.singapore.rds.aliyuncs.com:3717';
            $demo_user = 'user********';
            $demo_password = '123456';
            $demo_db = 'admin';
            # 根据实例信息构造mongodb connection string
            # mongodb://[username:password@]host[:port][,host2[:port2],...[,hostN[:portN]]][/[database]]
            $demo_uri = 'mongodb://' . $demo_user . ':' . $demo_password . '@' .
              $demo_seed . ',' . '/' . $demo_db;
            $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";
            }
        ?>

Java 连接示例

相关链接:

  1. 获取云数据库 MongoDB 连接信息。
  2. Java Demo Code。
    • Maven配置。
          <dependencies>
              <dependency>
                  <groupId>org.mongodb</groupId>
                  <artifactId>mongo-java-driver</artifactId>
                  <version>3.0.4</version>
              </dependency>
          </dependencies>
    • Java Code。
          import java.util.ArrayList;
          import java.util.List;
          import java.util.UUID;
          import org.bson.BsonDocument;
          import org.bson.BsonString;
          import org.bson.Document;
          import com.mongodb.MongoClient;
          import com.mongodb.MongoClientOptions;
          import com.mongodb.MongoClientURI;
          import com.mongodb.MongoCredential;
          import com.mongodb.ServerAddress;
          import com.mongodb.client.MongoCollection;
          import com.mongodb.client.MongoCursor;
          import com.mongodb.client.MongoDatabase;
          public class Main {
              public static ServerAddress seed = new ServerAddress("dds-t4n********.mongodb.singapore.rds.aliyuncs.com", 3717);
              public static String username = "user********";
              public static String password = "123456";
              public static String DEFAULT_DB = "admin";
              public static String DEMO_DB = "test";
              public static String DEMO_COLL = "testColl";
              public static MongoClient createMongoDBClient() {
                  // 构建Seed列表
                  List<ServerAddress> seedList = new ArrayList<ServerAddress>();
                  seedList.add(seed);
                  // 构建鉴权信息
                  List<MongoCredential> credentials = new ArrayList<MongoCredential>();
                  credentials.add(MongoCredential.createScramSha1Credential(username,
                          DEFAULT_DB, password.toCharArray()));
              public static MongoClient createMongoDBClientWithURI() {
                  //另一种通过URI初始化
                  //mongodb://[username:password@]host[:port][/[database]]
                  MongoClientURI connectionString = new MongoClientURI("mongodb://" + username + ":" + password + "@" + 
                                                                       seed + "/" + 
                                                                       DEFAULT_DB);
                  return new MongoClient(connectionString);
              }
              public static void main(String args[]) {
                  MongoClient client = createMongoDBClient();
                  //or
                  //MongoClient client = createMongoDBClientWithURI();
                  try {
                      // 取得Collecton句柄
                      MongoDatabase database = client.getDatabase(DEMO_DB);
                      MongoCollection<Document> collection = database.getCollection(DEMO_COLL);
                      // 插入数据
                      Document doc = new Document();
                      String demoname = "JAVA:" + UUID.randomUUID();
                      doc.append("DEMO", demoname);
                      doc.append("MESG", "Hello AliCoudDB For MongoDB");
                      collection.insertOne(doc);
                      System.out.println("insert document: " + doc);
                      // 读取数据
                      BsonDocument filter = new BsonDocument();
                      filter.append("DEMO", new BsonString(demoname));
                      MongoCursor<Document> cursor = collection.find(filter).iterator();
                      while (cursor.hasNext()) {
                          System.out.println("find document: " + cursor.next());
                      }       
                  } finally {
                      //关闭Client,释放资源
                      client.close();
                  }
                  return ;
              }
          }

Python 连接示例

  1. 安装pymongo。
        pip install pymongo
  2. 获取云数据库MongoDB连接信息。
  3. Python Demo Code。
        import uuid
            from pymongo import MongoClient
            #两地址
            CONN_ADDR = 'dds-t4n********.mongodb.singapore.rds.aliyuncs.com:3717'
            username = 'user********'
            password = '123456'
            #获取mongoclient
            client = MongoClient([CONN_ADDR])
            #授权. 这里的user基于admin数据库授权
            client.admin.authenticate(username, password)
            #使用test数据库的collection:testColl做例子, 插入doc, 然后根据DEMO名查找
            demo_name = 'python-' + str(uuid.uuid1())
            print 'demo_name:', demo_name
            doc = dict(DEMO=demo_name, MESG="Hello ApsaraDB For MongoDB")
            doc_id = client.test.testColl.insert(doc)
            print 'doc_id:', doc_id
            for d in client.test.testColl.find(dict(DEMO=demo_name)):
                print 'find documents:', d

C# 连接示例

相关链接:MongoDB C# Driver

  1. 获取云数据库 MongoDB 连接信息。详情请参见副本集实例连接说明
  2. C# Demo Code。
    using MongoDB.Driver;
    using System;
    using System.Collections.Generic;
    
    namespace Aliyun
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Mongo 实例信息
                const string host = "dds-t4n**********-pub.mongodb.singapore.rds.aliyuncs.com";
                const int port = 3717;
                const string admin = "admin";
                const string userName = "user********";
                const string passwd = "123456";
    
                try
                {
                    Console.WriteLine("开始连接.......");
                    MongoClientSettings settings = new MongoClientSettings();
                    List<MongoServerAddress> servers = new List<MongoServerAddress>();
                    servers.Add(new MongoServerAddress(host, port));
                    settings.Servers = servers;
                    //设置超时时间为3秒
                    settings.ConnectTimeout = new TimeSpan(0, 0, 0, 3, 0);
                    MongoCredential credentials = MongoCredential.CreateCredential(admin, userName, passwd);
                    settings.Credential = credentials;
                    MongoClient client = new MongoClient(settings);
                    var server = client.GetServer();
                    MongoDatabase database = server.GetDatabase("test");
                    var collection = database.GetCollection<User>("test_collection");
                    User user = new User();
                    user.id = "3";
                    user.name = "mongo_test";
                    user.sex = "女";
                    //插入数据user
                    collection.Insert(user);
                    //获取一条数据
                    User result = collection.FindOne();
                    Console.WriteLine("id:" + result.id + " name:" + result.name + " sex:" + result.sex);
                    Console.WriteLine("连接成功.........");
                }
                catch (Exception e)
                {
                    Console.WriteLine("连接异常:" + e.Message);
    
                }
            }
        }
        class User
        {
            public string id { set; get; }
            public string name { set; get; }
            public string sex { set; get; }
        }
    }