gpt4 book ai didi

node.js - 从 Node 连接到 Azure CosmosDB Mongodb 数据库

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:15 25 4
gpt4 key购买 nike

我正在尝试从 Node 应用程序连接到 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 的选项的良好教程/文档。

最佳答案

我只是按照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

关于node.js - 从 Node 连接到 Azure CosmosDB Mongodb 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55523738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com