[英]Why am I getting this deprecated warning?! MongoDB
我在 NodeJS 中使用 MongoDB,
const { MongoClient, ObjectId } = require("mongodb");
const MONGO_URI = `mongodb://xxx:xxx@xxx/?authSource=xxx`; // prettier-ignore
class MongoLib {
constructor() {
this.client = new MongoClient(MONGO_URI, {
useNewUrlParser: true,
});
this.dbName = DB_NAME;
}
connect() {
return new Promise((resolve, reject) => {
this.client.connect(error => {
if (error) {
reject(error);
}
resolve(this.client.db(this.dbName));
});
});
}
async getUser(collection, username) {
return this.connect().then(db => {
return db
.collection(collection)
.find({ username })
.toArray();
});
}
}
let c = new MongoLib();
c.getUser("users", "pepito").then(result => console.log(result));
c.getUser("users", "pepito").then(result => console.log(result));
當執行最后一個 c.getUser 語句時(也就是說,當我進行第二次連接時)Mongodb 輸出此警告:
the options [servers] is not supported
the options [caseTranslate] 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]
但我沒有使用任何已棄用的選項。 有任何想法嗎?
編輯
在評論中與molank進行了一些討論后,看起來從同一台服務器打開多個連接不是一個好習慣,所以也許這就是警告想要說的(我認為很糟糕)。 所以如果你有同樣的問題,保存連接而不是 mongo 客戶端。
從https://jira.mongodb.org/browse/NODE-1868轉帖:
棄用消息可能是因為client.connect
被多次調用。 總體而言,當前(從驅動程序v3.1.13
)多次調用client.connect
具有未定義的行為,不建議這樣做。 重要的是要注意,一旦從connect
返回的承諾解析,客戶端將保持連接,直到您調用client.close
:
const client = new MongoClient(...);
client.connect().then(() => {
// client is now connected.
return client.db('foo').collection('bar').insertOne({
}).then(() => {
// client is still connected.
return client.close();
}).then(() => {
// client is no longer connected. attempting to use it will result in undefined behavior.
});
默認情況下,客戶端與它所連接的每個服務器保持多個連接,並可用於多個同時操作*。 您應該可以運行client.connect
一次,然后在客戶端對象上運行您的操作
* 請注意,客戶端不是線程安全或分叉安全的,因此不能跨分叉共享,並且與節點的cluster
或worker_threads
模塊不兼容。
函數.connect()
接受3 個參數並定義為MongoClient.connect(url[, options], callback)
。 所以你需要先提供一個 URL,然后是選項,然后才給它回調。 這是文檔中的一個示例
MongoClient.connect("mongodb://localhost:27017/integration_tests", { native_parser: true }, function (err, db) {
assert.equal(null, err);
db.collection('mongoclient_test').update({ a: 1 }, { b: 1 }, { upsert: true }, function (err, result) {
assert.equal(null, err);
assert.equal(1, result);
db.close();
});
});
另一種方法,因為您已經創建了MongoClient
是使用.open
代替。 它只需要一個 callback ,但你從你創建的mongoClient
(this.client)
調用它。 你可以這樣使用它
this.client.open(function(err, mongoclient) {
// Do stuff
});
請務必查看MongoClient 文檔,您會發現很多很好的示例,可以更好地指導您。
poolSize
已棄用,請使用maxPoolSize
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.