0

I am trying to move code related to DB calls outside from my app to database functions. But each time I got an error : TypeError: db.conversations is not a function.

This is how my function looks like in system.js table:

function (input) {
    var collection = db.collection("conversations");
    var query = { conversationNotificationId: input };
    return collection.findOne(query);
}

Then I am trying to call it:

db.eval("myFunction('test')")

Here is full error output:

"errmsg" : "TypeError: db.collection is not a function",

What am I doing wrong ?

KaraKaplanKhan
  • 734
  • 1
  • 14
  • 31
kkost
  • 3,640
  • 5
  • 41
  • 72
  • 1
    [`eval()`](https://docs.mongodb.com/manual/reference/method/db.eval/) is deprecated. And **will be removed in the next major release**, which will be here soon enough. What exactly do you think you want to do here? And it's `getCollection()` in shell and database methods anyway. But you're basically doing the wrong thing. Write these methods in your application code instead. MongoDB does not have "stored procedures", though looking for something like that probably led you to `eval()`. It's not true. The other thing to read into this that server execution of JavaScript is going away as well. – Neil Lunn Mar 28 '19 at 08:26
  • @NeilLunn my main purpose is to move query code to MongoDB functions. So from my app, I will just call a remote function without building any query as far as it will be a part of a function on a MongoDB server side. Is this a good idea? – kkost Mar 28 '19 at 08:30
  • 1
    Yeah, well .. Read the additional information in the initial comment I added since you initially read it. Don't use server side functions. – Neil Lunn Mar 28 '19 at 08:30
  • @NeilLunn sorry, I've answered on your comment before you edit it. I've read a lot of info related to MongoDB functions but I didn't see any kind of restrictions and notification that I don't need to use it. From your comment, I make a conclusion that mongo functions will be removed with `eval` command together. Correct? – kkost Mar 28 '19 at 08:35
  • That's what deprecated means. By MongoDB 5.0 it will not be there at all ( or that's what's supposed to happen ). The `group()` method was another "JavaScript runner" which has already been removed. And I would suspect `mapReduce()` to follow suit, now that most of it's reasons for existing are covered within `aggregate()` and it's native commands. The actual embedded JavaScript engine is pretty old, and AFAIK there are no plans to update it. All roadsigns lead to **complete removal** of server side JavaScript. So don't plan on using it. – Neil Lunn Mar 28 '19 at 08:44

0 Answers0