Tuesday, 23 July 2019

mongodb nodejs driver await .



can i use await?

according to below links , yes, you can:

https://stackoverflow.com/questions/47370487/node-js-mongodb-driver-async-await-queries

http://mongodb.github.io/node-mongodb-native/2.2/reference/ecmascript6/


but await only works for creating connecting:


await MongoClient.connect(this.connectionString, {'useNewUrlParser': true});


it doesn't work with update insert functions.....  don't know why. (maybe i should use updateOne?)

so i have to use co and generator as the official document suggested:

http://mongodb.github.io/node-mongodb-native/2.2/reference/ecmascript6/crud/


note if you run the sample code with 3.+ mongo driver, then you cannot put the database name into the URI, you need to call : client.db function to get the db instance :

var client = yield MongoClient.connect(connectionString)
var db = client.db(dbName);

No comments:

Post a comment