从节点连接到Azure CosmosDB Mongodb数据库

时间:2019-04-04 19:38:45

标签: node.js mongodb azure mongoose azure-cosmosdb

我正在尝试从节点应用程序连接到Azure CosmosDB-MongoDb。

和以下代码:

我正在使用具有以下内容的连接字符串:

var url = mongodb://<cosmosdb-name>:<primary_master_key>@<cosmosdb-name>.documents.azure.com:10255/?ssl=true&replicaSet=globaldb'

const { MongoClient } = require('mongodb')
const mongodbClient = new MongoClient(url, { useNewUrlParser: true })
const db = await mongodbClient.connect()
const database = db.db(<databasename>)

但是当我尝试使用find或get进行读取时,代码将起作用,并且可以获取数据,但是我得到了以下警告:

the options [servers] is not supported
the options [sslverifycertificate] is not supported
the options [caseTranslate] is not supported
the options [credentials] is not supported
the options [username] is not supported
the server/replset/mongos/db options are deprecated, all their options are supported at the top level of the options object [poolSize,ssl,sslValidate,sslCA,sslCert,sslKey,sslPass,sslCRL,autoReconnect,noDelay,keepAlive,keepAliveInitialDelay,connectTimeoutMS,family,socketTimeoutMS,reconnectTries,reconnectInterval,ha,haInterval,replicaSet,secondaryAcceptableLatencyMS,acceptableLatencyMS,connectWithNoPrimary,authSource,w,wtimeout,j,forceServerObjectId,serializeFunctions,ignoreUndefined,raw,bufferMaxEntries,readPreference,pkFactory,promiseLibrary,readConcern,maxStalenessSeconds,loggerLevel,logger,promoteValues,promoteBuffers,promoteLongs,domainsEnabled,checkServerIdentity,validateOptions,appname,auth,user,password,authMechanism,compression,fsync,readPreferenceTags,numberOfRetries,auto_reconnect,minSize,monitorCommands,retryWrites,useNewUrlParser,useUnifiedTopology,serverSelectionTimeoutMS,useRecoveryToken]
the options [source] is not supported
the options [mechanism] is not supported
the options [mechanismProperties] is not supported

此外,我找不到有关可用于连接云上mongodb的选项的良好教程/文档。

1 个答案:

答案 0 :(得分:-1)

我只需按照npm mongodb guidelines中的示例代码在我的cosmos db mongo api中成功找到数据。请参考它。

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://***:***@***.documents.azure.com:10255/?ssl=true&replicaSet=globaldb';

// Database Name
const dbName = 'db';

// Use connect method to connect to the server
MongoClient.connect(url, 
  {useNewUrlParser: true},
function(err, client) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  findDocuments(db, function() {
      client.close();
    });
});

const findDocuments = function(db, callback) {
  // Get the documents collection
  const collection = db.collection('jay');
  // Find some documents
  collection.find({}).toArray(function(err, docs) {
    assert.equal(err, null);
    console.log("Found the following records");
    console.log(docs)
    callback(docs);
  });
}

输出:

enter image description here