db.eval async/await functionality

41 views
Skip to first unread message

jan...@princeton.edu

unread,
Jul 24, 2018, 10:10:13 AM7/24/18
to mongodb-user
Hi, I am trying to execute a db.collection.aggregate() query within a call to db.eval(). I am using eval() because I am making a dynamic number of lookups, so I generate the query by concatenating relevant strings. The query works perfectly when I manually remove the quotes from the string:

await db.collection.aggregate([<<relevant fields>>]).toArray();


var query = "await db.collection.aggregate([<<relevant fields>>]).toArray();"

Unfortunately, it does not work when I am using the string in a call to db.eval(). With the query as what is listed above, I have tried this:
db.eval('async function(){' + query + ' return;}', function(err, result) {
console.log('the result is: ', result);
});

I've also tried removing the word "async," and this still has not worked. How do I ensure that the function will finish aggregating before returning? Thanks.

jan...@princeton.edu

unread,
Jul 24, 2018, 10:24:09 AM7/24/18
to mongodb-user
I just noticed that db.eval() is deprecated and planned for removal. The alternative is "implement the equivalent queries/operations using the normal MongoDB query language and client driver API." How can I do this using a string query? 

Kevin Adistambha

unread,
Jul 30, 2018, 3:19:47 AM7/30/18
to mongodb-user

Hi,

It sounds like you’re trying to build a MongoDB query using a string, then executing that string using db.eval() similar to building an SQL query. Is this correct?

Note that unlike SQL, MongoDB query language was designed with driver usage in mind, so you should be able to programmatically build an aggregation pipeline without resorting to putting strings together.

Could you provide a concrete example of what you mean by “dynamic number of lookups” and why you think it cannot be done programmatically? What are the relevant fields, and what output are you expecting?

Please also post your MongoDB version and your driver version.

Best regards
Kevin

Reply all
Reply to author
Forward
0 new messages