Node.js mongodb driver async/await queries
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a node.js application using mongodb native driver.
In the process of migrating my application code to async/await using node v8.9.1, I am struggling to find an elegant way for the mongodb queries.
The major problem with mongodb driver is, that all queries are using callbacks where promises functions are mandatory for the async methods.
Alternatives:
mongoose- promises queries deprecated and it forces using Schema model which is a little overhead for my app.
mongoist- allegedly great, since it built with async/await in mind and fully promise, but errors with SSL connection to mongodb and poor documentations- drew me away from this solution.
The only workaround I succeeded to implement in an elegant way is using callback-promise npm package to convert mongodb driver API to fully promise.
Any fresh ideas for an elegant high performance way?
javascript node.js mongodb mongoose async-await
add a comment |
I have a node.js application using mongodb native driver.
In the process of migrating my application code to async/await using node v8.9.1, I am struggling to find an elegant way for the mongodb queries.
The major problem with mongodb driver is, that all queries are using callbacks where promises functions are mandatory for the async methods.
Alternatives:
mongoose- promises queries deprecated and it forces using Schema model which is a little overhead for my app.
mongoist- allegedly great, since it built with async/await in mind and fully promise, but errors with SSL connection to mongodb and poor documentations- drew me away from this solution.
The only workaround I succeeded to implement in an elegant way is using callback-promise npm package to convert mongodb driver API to fully promise.
Any fresh ideas for an elegant high performance way?
javascript node.js mongodb mongoose async-await
@MikaS Is seems to require a ‘co’ package. I am basically looking for a full promise native library
– Ido Lev
Nov 18 '17 at 21:45
add a comment |
I have a node.js application using mongodb native driver.
In the process of migrating my application code to async/await using node v8.9.1, I am struggling to find an elegant way for the mongodb queries.
The major problem with mongodb driver is, that all queries are using callbacks where promises functions are mandatory for the async methods.
Alternatives:
mongoose- promises queries deprecated and it forces using Schema model which is a little overhead for my app.
mongoist- allegedly great, since it built with async/await in mind and fully promise, but errors with SSL connection to mongodb and poor documentations- drew me away from this solution.
The only workaround I succeeded to implement in an elegant way is using callback-promise npm package to convert mongodb driver API to fully promise.
Any fresh ideas for an elegant high performance way?
javascript node.js mongodb mongoose async-await
I have a node.js application using mongodb native driver.
In the process of migrating my application code to async/await using node v8.9.1, I am struggling to find an elegant way for the mongodb queries.
The major problem with mongodb driver is, that all queries are using callbacks where promises functions are mandatory for the async methods.
Alternatives:
mongoose- promises queries deprecated and it forces using Schema model which is a little overhead for my app.
mongoist- allegedly great, since it built with async/await in mind and fully promise, but errors with SSL connection to mongodb and poor documentations- drew me away from this solution.
The only workaround I succeeded to implement in an elegant way is using callback-promise npm package to convert mongodb driver API to fully promise.
Any fresh ideas for an elegant high performance way?
javascript node.js mongodb mongoose async-await
javascript node.js mongodb mongoose async-await
edited Jan 13 at 6:37
Hendy Irawan
12.7k87688
12.7k87688
asked Nov 18 '17 at 20:03
Ido LevIdo Lev
2711210
2711210
@MikaS Is seems to require a ‘co’ package. I am basically looking for a full promise native library
– Ido Lev
Nov 18 '17 at 21:45
add a comment |
@MikaS Is seems to require a ‘co’ package. I am basically looking for a full promise native library
– Ido Lev
Nov 18 '17 at 21:45
@MikaS Is seems to require a ‘co’ package. I am basically looking for a full promise native library
– Ido Lev
Nov 18 '17 at 21:45
@MikaS Is seems to require a ‘co’ package. I am basically looking for a full promise native library
– Ido Lev
Nov 18 '17 at 21:45
add a comment |
8 Answers
8
active
oldest
votes
This is the smallest piece of code I found that is compatible with Mongo3 and async/await.
Enjoy!
module.exports = {
myFunction: async (query) => {
let db, client;
try {
client = await MongoClient.connect(process.env.MONGODB_CONNECTION_STRING, { useNewUrlParser: true });
db = client.db(dbName);
return await db.collection(collectionName).find(query).toArray();
} finally {
client.close();
}
}
}
[duplicate answer] this answer does not add any unique value to other answers, and executable code snippet does not working, its usage is wrong.
– Serhat Ates
Nov 16 '18 at 8:09
Yes probably, I just found the others answers not concise enough
– Sovattha Sok
Nov 16 '18 at 12:06
add a comment |
Edit: 'mongodb' v3.x
according to mongoDB ES6 future
you can use this way;
let MongoClient = require('mongodb').MongoClient;
const connectionString = 'mongodb://localhost:27017';
(async () => {
let client = await MongoClient.connect(connectionString,
{ useNewUrlParser: true });
let db = client.db('dbName');
try {
const res = await db.collection("collectionName").updateOne({
"someKey": someValue
}, { $set: someObj }, { upsert: true });
console.log(`res => ${JSON.stringify(res)}`);
}
finally {
client.close();
}
})()
.catch(err => console.error(err));
1
This is beautiful and is exactly what I was looking for. Thank you!
– Spike
Mar 24 '18 at 14:44
Since Version 3, MongoClient returns a client, not a db object!
– wiesson
May 7 '18 at 18:03
5
This doesn't work anymore. Can we get an update for recent versions of mongodb please?
– pguardiario
Jul 14 '18 at 6:31
add a comment |
Thanks. Working great with ES6:
const middleWare = require('middleWare');
const MONGO = require('mongodb').MongoClient;
router.get('/', middleWare(async (req, res, next) => {
const db = await MONGO.connect(url);
const MyCollection = db.collection('MyCollection');
const result = await MyCollection.find(query).toArray();
res.send(result);
}))
1
this is a great answer. For anyone else trying to figure out therequire('middleware')step, here is a great guide: medium.com/@Abazhenov/…
– JP Lew
Jan 26 '18 at 7:06
This doesn't work anymore either. Any chance of an update?
– pguardiario
Jul 14 '18 at 6:32
I neglected mongodb and using mongoose now. It is highly recommend since lots of updates and promises support.
– Ido Lev
Jul 15 '18 at 9:28
add a comment |
If u don't pass a callback, mongodb client returns a promise.
The official MongoDB Node.js driver provides both callback based as well as Promised based interaction with MongoDB allowing applications to take full advantage of the new features in ES6
From the official
docs
add a comment |
I'm posting this as an answer because I can't comment on Ido Lev's answer. I will move this as soon as I have reached 50 reputation.
Don't forget to close the db connection. Otherwise it's possible that your application can't connect to the db because of too many open connections (happened to me a week ago).
Your query may succeed or fail, so it makes sense to close the connection in a finally-block.
const db = await MongoClient.connect(url);
try {
const stuff = await db.collection("Stuff").find({});
// Do something with the result of the query
} finally {
db.close();
}
Update: It seems that this also didn't fix my problem. Some people say that you don't even need to close the connection manually. It seems like it's best to reuse your connection across you application if possible.
1
Yes! I'm trying to open a connection and to reuse it for all the database action needed to process the current request. AFAIK opening a connection is an expensive operation while reads and writes are much lighter and can can be done through a single connection. Most examples around connect, update and close all in the same code block. I would like to store my open connection in a shared context to make it available to all the functions.
– Juan Lanus
Sep 12 '18 at 15:16
add a comment |
mongoose find Query Using async/await
dont Use mongoose.connect in case of async/await
var router = require("express").Router()
var mongoose = require("mongoose")
var await = require("await")
var async = require("async")
var mongoUrl = "mongodb://localhost:27017/ekaushalnsdc"
router.get("/async/await/find",async(req, res, next) => {
try {
var db = await mongoose.createConnection(mongoUrl)
var colName = db.collection('collectionName')
var result = await colName.find({}).toArray()
res.json(result)
}catch(ex) {
res.json(ex.message)
}
})
Thank you for this comment. I think that anyway the connection should be created on server initiation, so it would be provided globally for all the modules.
– Ido Lev
Jul 5 '18 at 15:15
add a comment |
If you want to work with cursor without unloading to Array, you can't use await with find() or aggregate() functions, then you have to use the code:
UPD by Usas:
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('name').aggregate(
[
{
"$match": {code: 10}
},
{
"$count": "count"
}
],
{
"allowDiskUse": false
}
)
for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {
console.log('aggregate:', doc.count);
}
2
@Pax_Beach - please edit your answer so I'll can revote it as useful. See Usas comment above. Need to be edited (with no changes) due to Usas explanation for the rare cases where your answer is relevant.
– Ido Lev
Jan 11 at 11:58
add a comment |
(Based on Pax Beach's answer. It had been downvoted, and I wanted to add a comment explaining why in some situations, Pat's answer is the best. I don't have enough rep to add comments.)
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('someCollection').find({})
for (let doc = await cursor.next(); doc; doc = await cursor.next()) {
// Process the document.
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47370487%2fnode-js-mongodb-driver-async-await-queries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is the smallest piece of code I found that is compatible with Mongo3 and async/await.
Enjoy!
module.exports = {
myFunction: async (query) => {
let db, client;
try {
client = await MongoClient.connect(process.env.MONGODB_CONNECTION_STRING, { useNewUrlParser: true });
db = client.db(dbName);
return await db.collection(collectionName).find(query).toArray();
} finally {
client.close();
}
}
}
[duplicate answer] this answer does not add any unique value to other answers, and executable code snippet does not working, its usage is wrong.
– Serhat Ates
Nov 16 '18 at 8:09
Yes probably, I just found the others answers not concise enough
– Sovattha Sok
Nov 16 '18 at 12:06
add a comment |
This is the smallest piece of code I found that is compatible with Mongo3 and async/await.
Enjoy!
module.exports = {
myFunction: async (query) => {
let db, client;
try {
client = await MongoClient.connect(process.env.MONGODB_CONNECTION_STRING, { useNewUrlParser: true });
db = client.db(dbName);
return await db.collection(collectionName).find(query).toArray();
} finally {
client.close();
}
}
}
[duplicate answer] this answer does not add any unique value to other answers, and executable code snippet does not working, its usage is wrong.
– Serhat Ates
Nov 16 '18 at 8:09
Yes probably, I just found the others answers not concise enough
– Sovattha Sok
Nov 16 '18 at 12:06
add a comment |
This is the smallest piece of code I found that is compatible with Mongo3 and async/await.
Enjoy!
module.exports = {
myFunction: async (query) => {
let db, client;
try {
client = await MongoClient.connect(process.env.MONGODB_CONNECTION_STRING, { useNewUrlParser: true });
db = client.db(dbName);
return await db.collection(collectionName).find(query).toArray();
} finally {
client.close();
}
}
}
This is the smallest piece of code I found that is compatible with Mongo3 and async/await.
Enjoy!
module.exports = {
myFunction: async (query) => {
let db, client;
try {
client = await MongoClient.connect(process.env.MONGODB_CONNECTION_STRING, { useNewUrlParser: true });
db = client.db(dbName);
return await db.collection(collectionName).find(query).toArray();
} finally {
client.close();
}
}
}
edited Nov 16 '18 at 12:04
answered Nov 15 '18 at 11:16
Sovattha SokSovattha Sok
40149
40149
[duplicate answer] this answer does not add any unique value to other answers, and executable code snippet does not working, its usage is wrong.
– Serhat Ates
Nov 16 '18 at 8:09
Yes probably, I just found the others answers not concise enough
– Sovattha Sok
Nov 16 '18 at 12:06
add a comment |
[duplicate answer] this answer does not add any unique value to other answers, and executable code snippet does not working, its usage is wrong.
– Serhat Ates
Nov 16 '18 at 8:09
Yes probably, I just found the others answers not concise enough
– Sovattha Sok
Nov 16 '18 at 12:06
[duplicate answer] this answer does not add any unique value to other answers, and executable code snippet does not working, its usage is wrong.
– Serhat Ates
Nov 16 '18 at 8:09
[duplicate answer] this answer does not add any unique value to other answers, and executable code snippet does not working, its usage is wrong.
– Serhat Ates
Nov 16 '18 at 8:09
Yes probably, I just found the others answers not concise enough
– Sovattha Sok
Nov 16 '18 at 12:06
Yes probably, I just found the others answers not concise enough
– Sovattha Sok
Nov 16 '18 at 12:06
add a comment |
Edit: 'mongodb' v3.x
according to mongoDB ES6 future
you can use this way;
let MongoClient = require('mongodb').MongoClient;
const connectionString = 'mongodb://localhost:27017';
(async () => {
let client = await MongoClient.connect(connectionString,
{ useNewUrlParser: true });
let db = client.db('dbName');
try {
const res = await db.collection("collectionName").updateOne({
"someKey": someValue
}, { $set: someObj }, { upsert: true });
console.log(`res => ${JSON.stringify(res)}`);
}
finally {
client.close();
}
})()
.catch(err => console.error(err));
1
This is beautiful and is exactly what I was looking for. Thank you!
– Spike
Mar 24 '18 at 14:44
Since Version 3, MongoClient returns a client, not a db object!
– wiesson
May 7 '18 at 18:03
5
This doesn't work anymore. Can we get an update for recent versions of mongodb please?
– pguardiario
Jul 14 '18 at 6:31
add a comment |
Edit: 'mongodb' v3.x
according to mongoDB ES6 future
you can use this way;
let MongoClient = require('mongodb').MongoClient;
const connectionString = 'mongodb://localhost:27017';
(async () => {
let client = await MongoClient.connect(connectionString,
{ useNewUrlParser: true });
let db = client.db('dbName');
try {
const res = await db.collection("collectionName").updateOne({
"someKey": someValue
}, { $set: someObj }, { upsert: true });
console.log(`res => ${JSON.stringify(res)}`);
}
finally {
client.close();
}
})()
.catch(err => console.error(err));
1
This is beautiful and is exactly what I was looking for. Thank you!
– Spike
Mar 24 '18 at 14:44
Since Version 3, MongoClient returns a client, not a db object!
– wiesson
May 7 '18 at 18:03
5
This doesn't work anymore. Can we get an update for recent versions of mongodb please?
– pguardiario
Jul 14 '18 at 6:31
add a comment |
Edit: 'mongodb' v3.x
according to mongoDB ES6 future
you can use this way;
let MongoClient = require('mongodb').MongoClient;
const connectionString = 'mongodb://localhost:27017';
(async () => {
let client = await MongoClient.connect(connectionString,
{ useNewUrlParser: true });
let db = client.db('dbName');
try {
const res = await db.collection("collectionName").updateOne({
"someKey": someValue
}, { $set: someObj }, { upsert: true });
console.log(`res => ${JSON.stringify(res)}`);
}
finally {
client.close();
}
})()
.catch(err => console.error(err));
Edit: 'mongodb' v3.x
according to mongoDB ES6 future
you can use this way;
let MongoClient = require('mongodb').MongoClient;
const connectionString = 'mongodb://localhost:27017';
(async () => {
let client = await MongoClient.connect(connectionString,
{ useNewUrlParser: true });
let db = client.db('dbName');
try {
const res = await db.collection("collectionName").updateOne({
"someKey": someValue
}, { $set: someObj }, { upsert: true });
console.log(`res => ${JSON.stringify(res)}`);
}
finally {
client.close();
}
})()
.catch(err => console.error(err));
edited Nov 16 '18 at 8:04
answered Mar 16 '18 at 13:11
Serhat AtesSerhat Ates
394313
394313
1
This is beautiful and is exactly what I was looking for. Thank you!
– Spike
Mar 24 '18 at 14:44
Since Version 3, MongoClient returns a client, not a db object!
– wiesson
May 7 '18 at 18:03
5
This doesn't work anymore. Can we get an update for recent versions of mongodb please?
– pguardiario
Jul 14 '18 at 6:31
add a comment |
1
This is beautiful and is exactly what I was looking for. Thank you!
– Spike
Mar 24 '18 at 14:44
Since Version 3, MongoClient returns a client, not a db object!
– wiesson
May 7 '18 at 18:03
5
This doesn't work anymore. Can we get an update for recent versions of mongodb please?
– pguardiario
Jul 14 '18 at 6:31
1
1
This is beautiful and is exactly what I was looking for. Thank you!
– Spike
Mar 24 '18 at 14:44
This is beautiful and is exactly what I was looking for. Thank you!
– Spike
Mar 24 '18 at 14:44
Since Version 3, MongoClient returns a client, not a db object!
– wiesson
May 7 '18 at 18:03
Since Version 3, MongoClient returns a client, not a db object!
– wiesson
May 7 '18 at 18:03
5
5
This doesn't work anymore. Can we get an update for recent versions of mongodb please?
– pguardiario
Jul 14 '18 at 6:31
This doesn't work anymore. Can we get an update for recent versions of mongodb please?
– pguardiario
Jul 14 '18 at 6:31
add a comment |
Thanks. Working great with ES6:
const middleWare = require('middleWare');
const MONGO = require('mongodb').MongoClient;
router.get('/', middleWare(async (req, res, next) => {
const db = await MONGO.connect(url);
const MyCollection = db.collection('MyCollection');
const result = await MyCollection.find(query).toArray();
res.send(result);
}))
1
this is a great answer. For anyone else trying to figure out therequire('middleware')step, here is a great guide: medium.com/@Abazhenov/…
– JP Lew
Jan 26 '18 at 7:06
This doesn't work anymore either. Any chance of an update?
– pguardiario
Jul 14 '18 at 6:32
I neglected mongodb and using mongoose now. It is highly recommend since lots of updates and promises support.
– Ido Lev
Jul 15 '18 at 9:28
add a comment |
Thanks. Working great with ES6:
const middleWare = require('middleWare');
const MONGO = require('mongodb').MongoClient;
router.get('/', middleWare(async (req, res, next) => {
const db = await MONGO.connect(url);
const MyCollection = db.collection('MyCollection');
const result = await MyCollection.find(query).toArray();
res.send(result);
}))
1
this is a great answer. For anyone else trying to figure out therequire('middleware')step, here is a great guide: medium.com/@Abazhenov/…
– JP Lew
Jan 26 '18 at 7:06
This doesn't work anymore either. Any chance of an update?
– pguardiario
Jul 14 '18 at 6:32
I neglected mongodb and using mongoose now. It is highly recommend since lots of updates and promises support.
– Ido Lev
Jul 15 '18 at 9:28
add a comment |
Thanks. Working great with ES6:
const middleWare = require('middleWare');
const MONGO = require('mongodb').MongoClient;
router.get('/', middleWare(async (req, res, next) => {
const db = await MONGO.connect(url);
const MyCollection = db.collection('MyCollection');
const result = await MyCollection.find(query).toArray();
res.send(result);
}))
Thanks. Working great with ES6:
const middleWare = require('middleWare');
const MONGO = require('mongodb').MongoClient;
router.get('/', middleWare(async (req, res, next) => {
const db = await MONGO.connect(url);
const MyCollection = db.collection('MyCollection');
const result = await MyCollection.find(query).toArray();
res.send(result);
}))
answered Nov 19 '17 at 6:51
Ido LevIdo Lev
2711210
2711210
1
this is a great answer. For anyone else trying to figure out therequire('middleware')step, here is a great guide: medium.com/@Abazhenov/…
– JP Lew
Jan 26 '18 at 7:06
This doesn't work anymore either. Any chance of an update?
– pguardiario
Jul 14 '18 at 6:32
I neglected mongodb and using mongoose now. It is highly recommend since lots of updates and promises support.
– Ido Lev
Jul 15 '18 at 9:28
add a comment |
1
this is a great answer. For anyone else trying to figure out therequire('middleware')step, here is a great guide: medium.com/@Abazhenov/…
– JP Lew
Jan 26 '18 at 7:06
This doesn't work anymore either. Any chance of an update?
– pguardiario
Jul 14 '18 at 6:32
I neglected mongodb and using mongoose now. It is highly recommend since lots of updates and promises support.
– Ido Lev
Jul 15 '18 at 9:28
1
1
this is a great answer. For anyone else trying to figure out the
require('middleware') step, here is a great guide: medium.com/@Abazhenov/…– JP Lew
Jan 26 '18 at 7:06
this is a great answer. For anyone else trying to figure out the
require('middleware') step, here is a great guide: medium.com/@Abazhenov/…– JP Lew
Jan 26 '18 at 7:06
This doesn't work anymore either. Any chance of an update?
– pguardiario
Jul 14 '18 at 6:32
This doesn't work anymore either. Any chance of an update?
– pguardiario
Jul 14 '18 at 6:32
I neglected mongodb and using mongoose now. It is highly recommend since lots of updates and promises support.
– Ido Lev
Jul 15 '18 at 9:28
I neglected mongodb and using mongoose now. It is highly recommend since lots of updates and promises support.
– Ido Lev
Jul 15 '18 at 9:28
add a comment |
If u don't pass a callback, mongodb client returns a promise.
The official MongoDB Node.js driver provides both callback based as well as Promised based interaction with MongoDB allowing applications to take full advantage of the new features in ES6
From the official
docs
add a comment |
If u don't pass a callback, mongodb client returns a promise.
The official MongoDB Node.js driver provides both callback based as well as Promised based interaction with MongoDB allowing applications to take full advantage of the new features in ES6
From the official
docs
add a comment |
If u don't pass a callback, mongodb client returns a promise.
The official MongoDB Node.js driver provides both callback based as well as Promised based interaction with MongoDB allowing applications to take full advantage of the new features in ES6
From the official
docs
If u don't pass a callback, mongodb client returns a promise.
The official MongoDB Node.js driver provides both callback based as well as Promised based interaction with MongoDB allowing applications to take full advantage of the new features in ES6
From the official
docs
answered Nov 18 '17 at 20:47
SuryaSurya
416
416
add a comment |
add a comment |
I'm posting this as an answer because I can't comment on Ido Lev's answer. I will move this as soon as I have reached 50 reputation.
Don't forget to close the db connection. Otherwise it's possible that your application can't connect to the db because of too many open connections (happened to me a week ago).
Your query may succeed or fail, so it makes sense to close the connection in a finally-block.
const db = await MongoClient.connect(url);
try {
const stuff = await db.collection("Stuff").find({});
// Do something with the result of the query
} finally {
db.close();
}
Update: It seems that this also didn't fix my problem. Some people say that you don't even need to close the connection manually. It seems like it's best to reuse your connection across you application if possible.
1
Yes! I'm trying to open a connection and to reuse it for all the database action needed to process the current request. AFAIK opening a connection is an expensive operation while reads and writes are much lighter and can can be done through a single connection. Most examples around connect, update and close all in the same code block. I would like to store my open connection in a shared context to make it available to all the functions.
– Juan Lanus
Sep 12 '18 at 15:16
add a comment |
I'm posting this as an answer because I can't comment on Ido Lev's answer. I will move this as soon as I have reached 50 reputation.
Don't forget to close the db connection. Otherwise it's possible that your application can't connect to the db because of too many open connections (happened to me a week ago).
Your query may succeed or fail, so it makes sense to close the connection in a finally-block.
const db = await MongoClient.connect(url);
try {
const stuff = await db.collection("Stuff").find({});
// Do something with the result of the query
} finally {
db.close();
}
Update: It seems that this also didn't fix my problem. Some people say that you don't even need to close the connection manually. It seems like it's best to reuse your connection across you application if possible.
1
Yes! I'm trying to open a connection and to reuse it for all the database action needed to process the current request. AFAIK opening a connection is an expensive operation while reads and writes are much lighter and can can be done through a single connection. Most examples around connect, update and close all in the same code block. I would like to store my open connection in a shared context to make it available to all the functions.
– Juan Lanus
Sep 12 '18 at 15:16
add a comment |
I'm posting this as an answer because I can't comment on Ido Lev's answer. I will move this as soon as I have reached 50 reputation.
Don't forget to close the db connection. Otherwise it's possible that your application can't connect to the db because of too many open connections (happened to me a week ago).
Your query may succeed or fail, so it makes sense to close the connection in a finally-block.
const db = await MongoClient.connect(url);
try {
const stuff = await db.collection("Stuff").find({});
// Do something with the result of the query
} finally {
db.close();
}
Update: It seems that this also didn't fix my problem. Some people say that you don't even need to close the connection manually. It seems like it's best to reuse your connection across you application if possible.
I'm posting this as an answer because I can't comment on Ido Lev's answer. I will move this as soon as I have reached 50 reputation.
Don't forget to close the db connection. Otherwise it's possible that your application can't connect to the db because of too many open connections (happened to me a week ago).
Your query may succeed or fail, so it makes sense to close the connection in a finally-block.
const db = await MongoClient.connect(url);
try {
const stuff = await db.collection("Stuff").find({});
// Do something with the result of the query
} finally {
db.close();
}
Update: It seems that this also didn't fix my problem. Some people say that you don't even need to close the connection manually. It seems like it's best to reuse your connection across you application if possible.
edited May 9 '18 at 15:49
answered May 4 '18 at 15:25
0xC0DEBA5E0xC0DEBA5E
615
615
1
Yes! I'm trying to open a connection and to reuse it for all the database action needed to process the current request. AFAIK opening a connection is an expensive operation while reads and writes are much lighter and can can be done through a single connection. Most examples around connect, update and close all in the same code block. I would like to store my open connection in a shared context to make it available to all the functions.
– Juan Lanus
Sep 12 '18 at 15:16
add a comment |
1
Yes! I'm trying to open a connection and to reuse it for all the database action needed to process the current request. AFAIK opening a connection is an expensive operation while reads and writes are much lighter and can can be done through a single connection. Most examples around connect, update and close all in the same code block. I would like to store my open connection in a shared context to make it available to all the functions.
– Juan Lanus
Sep 12 '18 at 15:16
1
1
Yes! I'm trying to open a connection and to reuse it for all the database action needed to process the current request. AFAIK opening a connection is an expensive operation while reads and writes are much lighter and can can be done through a single connection. Most examples around connect, update and close all in the same code block. I would like to store my open connection in a shared context to make it available to all the functions.
– Juan Lanus
Sep 12 '18 at 15:16
Yes! I'm trying to open a connection and to reuse it for all the database action needed to process the current request. AFAIK opening a connection is an expensive operation while reads and writes are much lighter and can can be done through a single connection. Most examples around connect, update and close all in the same code block. I would like to store my open connection in a shared context to make it available to all the functions.
– Juan Lanus
Sep 12 '18 at 15:16
add a comment |
mongoose find Query Using async/await
dont Use mongoose.connect in case of async/await
var router = require("express").Router()
var mongoose = require("mongoose")
var await = require("await")
var async = require("async")
var mongoUrl = "mongodb://localhost:27017/ekaushalnsdc"
router.get("/async/await/find",async(req, res, next) => {
try {
var db = await mongoose.createConnection(mongoUrl)
var colName = db.collection('collectionName')
var result = await colName.find({}).toArray()
res.json(result)
}catch(ex) {
res.json(ex.message)
}
})
Thank you for this comment. I think that anyway the connection should be created on server initiation, so it would be provided globally for all the modules.
– Ido Lev
Jul 5 '18 at 15:15
add a comment |
mongoose find Query Using async/await
dont Use mongoose.connect in case of async/await
var router = require("express").Router()
var mongoose = require("mongoose")
var await = require("await")
var async = require("async")
var mongoUrl = "mongodb://localhost:27017/ekaushalnsdc"
router.get("/async/await/find",async(req, res, next) => {
try {
var db = await mongoose.createConnection(mongoUrl)
var colName = db.collection('collectionName')
var result = await colName.find({}).toArray()
res.json(result)
}catch(ex) {
res.json(ex.message)
}
})
Thank you for this comment. I think that anyway the connection should be created on server initiation, so it would be provided globally for all the modules.
– Ido Lev
Jul 5 '18 at 15:15
add a comment |
mongoose find Query Using async/await
dont Use mongoose.connect in case of async/await
var router = require("express").Router()
var mongoose = require("mongoose")
var await = require("await")
var async = require("async")
var mongoUrl = "mongodb://localhost:27017/ekaushalnsdc"
router.get("/async/await/find",async(req, res, next) => {
try {
var db = await mongoose.createConnection(mongoUrl)
var colName = db.collection('collectionName')
var result = await colName.find({}).toArray()
res.json(result)
}catch(ex) {
res.json(ex.message)
}
})
mongoose find Query Using async/await
dont Use mongoose.connect in case of async/await
var router = require("express").Router()
var mongoose = require("mongoose")
var await = require("await")
var async = require("async")
var mongoUrl = "mongodb://localhost:27017/ekaushalnsdc"
router.get("/async/await/find",async(req, res, next) => {
try {
var db = await mongoose.createConnection(mongoUrl)
var colName = db.collection('collectionName')
var result = await colName.find({}).toArray()
res.json(result)
}catch(ex) {
res.json(ex.message)
}
})
answered Jul 4 '18 at 14:07
muthukumarmuthukumar
369510
369510
Thank you for this comment. I think that anyway the connection should be created on server initiation, so it would be provided globally for all the modules.
– Ido Lev
Jul 5 '18 at 15:15
add a comment |
Thank you for this comment. I think that anyway the connection should be created on server initiation, so it would be provided globally for all the modules.
– Ido Lev
Jul 5 '18 at 15:15
Thank you for this comment. I think that anyway the connection should be created on server initiation, so it would be provided globally for all the modules.
– Ido Lev
Jul 5 '18 at 15:15
Thank you for this comment. I think that anyway the connection should be created on server initiation, so it would be provided globally for all the modules.
– Ido Lev
Jul 5 '18 at 15:15
add a comment |
If you want to work with cursor without unloading to Array, you can't use await with find() or aggregate() functions, then you have to use the code:
UPD by Usas:
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('name').aggregate(
[
{
"$match": {code: 10}
},
{
"$count": "count"
}
],
{
"allowDiskUse": false
}
)
for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {
console.log('aggregate:', doc.count);
}
2
@Pax_Beach - please edit your answer so I'll can revote it as useful. See Usas comment above. Need to be edited (with no changes) due to Usas explanation for the rare cases where your answer is relevant.
– Ido Lev
Jan 11 at 11:58
add a comment |
If you want to work with cursor without unloading to Array, you can't use await with find() or aggregate() functions, then you have to use the code:
UPD by Usas:
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('name').aggregate(
[
{
"$match": {code: 10}
},
{
"$count": "count"
}
],
{
"allowDiskUse": false
}
)
for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {
console.log('aggregate:', doc.count);
}
2
@Pax_Beach - please edit your answer so I'll can revote it as useful. See Usas comment above. Need to be edited (with no changes) due to Usas explanation for the rare cases where your answer is relevant.
– Ido Lev
Jan 11 at 11:58
add a comment |
If you want to work with cursor without unloading to Array, you can't use await with find() or aggregate() functions, then you have to use the code:
UPD by Usas:
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('name').aggregate(
[
{
"$match": {code: 10}
},
{
"$count": "count"
}
],
{
"allowDiskUse": false
}
)
for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {
console.log('aggregate:', doc.count);
}
If you want to work with cursor without unloading to Array, you can't use await with find() or aggregate() functions, then you have to use the code:
UPD by Usas:
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('name').aggregate(
[
{
"$match": {code: 10}
},
{
"$count": "count"
}
],
{
"allowDiskUse": false
}
)
for (let doc = await cursor.next(); doc != null; doc = await cursor.next()) {
console.log('aggregate:', doc.count);
}
edited Jan 11 at 12:35
answered Mar 15 '18 at 18:37
Pax BeachPax Beach
555814
555814
2
@Pax_Beach - please edit your answer so I'll can revote it as useful. See Usas comment above. Need to be edited (with no changes) due to Usas explanation for the rare cases where your answer is relevant.
– Ido Lev
Jan 11 at 11:58
add a comment |
2
@Pax_Beach - please edit your answer so I'll can revote it as useful. See Usas comment above. Need to be edited (with no changes) due to Usas explanation for the rare cases where your answer is relevant.
– Ido Lev
Jan 11 at 11:58
2
2
@Pax_Beach - please edit your answer so I'll can revote it as useful. See Usas comment above. Need to be edited (with no changes) due to Usas explanation for the rare cases where your answer is relevant.
– Ido Lev
Jan 11 at 11:58
@Pax_Beach - please edit your answer so I'll can revote it as useful. See Usas comment above. Need to be edited (with no changes) due to Usas explanation for the rare cases where your answer is relevant.
– Ido Lev
Jan 11 at 11:58
add a comment |
(Based on Pax Beach's answer. It had been downvoted, and I wanted to add a comment explaining why in some situations, Pat's answer is the best. I don't have enough rep to add comments.)
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('someCollection').find({})
for (let doc = await cursor.next(); doc; doc = await cursor.next()) {
// Process the document.
}
add a comment |
(Based on Pax Beach's answer. It had been downvoted, and I wanted to add a comment explaining why in some situations, Pat's answer is the best. I don't have enough rep to add comments.)
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('someCollection').find({})
for (let doc = await cursor.next(); doc; doc = await cursor.next()) {
// Process the document.
}
add a comment |
(Based on Pax Beach's answer. It had been downvoted, and I wanted to add a comment explaining why in some situations, Pat's answer is the best. I don't have enough rep to add comments.)
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('someCollection').find({})
for (let doc = await cursor.next(); doc; doc = await cursor.next()) {
// Process the document.
}
(Based on Pax Beach's answer. It had been downvoted, and I wanted to add a comment explaining why in some situations, Pat's answer is the best. I don't have enough rep to add comments.)
For the general case, the answers using toArray() are sufficient.
But when huge document collections are involved, using toArray() would exceed available RAM. Thus a "high performance" solution in those situations must not use toArray().
For those cases, you can use MongoDB streams, which works well, but even simpler than using streams is to:
const cursor = db.collection('someCollection').find({})
for (let doc = await cursor.next(); doc; doc = await cursor.next()) {
// Process the document.
}
answered Dec 31 '18 at 15:48
UsasUsas
566
566
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47370487%2fnode-js-mongodb-driver-async-await-queries%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@MikaS Is seems to require a ‘co’ package. I am basically looking for a full promise native library
– Ido Lev
Nov 18 '17 at 21:45