我在Node.js上使用Mongodb官方驱动程序,当我对数据库进行查询时,我收到很多警告,这些警告对我没有任何意义。
我读到这可能是在同一服务器上打开多个连接的问题,但是我看不到为什么收到警告,因为在代码中可以看到我在返回结果之前关闭了每个连接。
这是我与驱动程序建立的库:
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