Javascript 在node.js中获取Mongodb上不推荐使用的警告
我在Node.js上使用Mongodb官方驱动程序,当我对数据库进行查询时,我收到了很多对我来说毫无意义的警告 我了解到,在同一台服务器中打开多个连接可能会有问题,但我不明白为什么会收到警告,正如您在代码中看到的,在返回结果之前,我会关闭每个连接 这是我用驱动程序制作的库:Javascript 在node.js中获取Mongodb上不推荐使用的警告,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我在Node.js上使用Mongodb官方驱动程序,当我对数据库进行查询时,我收到了很多对我来说毫无意义的警告 我了解到,在同一台服务器中打开多个连接可能会有问题,但我不明白为什么会收到警告,正如您在代码中看到的,在返回结果之前,我会关闭每个连接 这是我用驱动程序制作的库: const {MongoClient, ObjectId} = require("mongodb"); const {mongoURI, mongoDbName} = require("../config/"); const
const {MongoClient, ObjectId} = require("mongodb");
const {mongoURI, mongoDbName} = require("../config/");
const debug = require("debug")("app:mongodb");
class MongoLib {
constructor(collection) {
this.client = new MongoClient(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
this.dbName = mongoDbName;
this.collection = collection;
}
connect() {
return new Promise((resolve, reject) => {
this.client.connect(error => {
if (error) {
reject(error);
} else {
debug("Connection to mongodb succesful");
resolve(this.client.db(this.dbName));
}
});
});
}
createOne(data) {
return this.connect().then(db =>
db
.collection(this.collection)
.insertOne(data)
.then(result =>
this.readOne({
_id: result.insertedId
}).then(async readed => {
await this.client
.close()
.then(() => debug("closed mongodb connection"));
return readed;
})
)
);
}
readAll(query = {}) {
return this.connect()
.then(db =>
db
.collection(this.collection)
.find(query)
.toArray()
)
.then(async data => {
await this.client
.close()
.then(() => debug("closed mongodb connection"));
return data;
});
}
readOne(query = {}) {
return this.connect()
.then(db => db.collection(this.collection).findOne(query))
.then(async data => {
await this.client
.close()
.then(() => debug("closed mongodb connection"));
return data;
});
}
readById(objectId) {
return this.readOne({_id: new ObjectId(objectId)});
}
updateOne(query = {}, data = {}) {
return this.connect().then(db =>
db
.collection(this.collection)
.updateOne(query, {$set: data})
.then(() =>
this.readOne({
...data
}).then(async data => {
await this.client
.close()
.then(() => debug("closed mongodb connection"));
return data;
})
)
);
}
updateOneById(objectId, data = {}) {
return this.updateOne({_id: new ObjectId(objectId)}, data);
}
removeOne(query = {}) {
return this.connect()
.then(db => db.collection(this.collection).deleteOne(query))
.then(async result => {
await this.client
.close()
.then(() => debug("closed mongodb connection"));
return result;
});
}
removeOneById(objectId) {
return this.removeOne({_id: new ObjectId(objectId)});
}
}
我收到以下警告:
the options [servers] is not supported
the options [caseTranslate] is not supported
the options [dbName] is not supported
the options [srvHost] is not supported
the options [credentials] is not supported
the options [username] is not supported
the options [source] is not supported
the options [mechanism] is not supported
the options [mechanismProperties] is not supported
我相信您的MongoClient不再需要这些选项。在选项中的某个地方,您正在设置这些选项。我会在代码库中搜索这些选项的文本条目,然后删除它们 我试图在NodeJSMongoDB文档中搜索答案,但他们没有特别提到这些。我知道我在升级MongoDB二进制文件和Java驱动程序时遇到了类似的情况。在升级版本时,我不得不在java驱动程序中使用不同的选项/方法 我看到了另一个问题: 这也可能有所帮助