1

Consider this dummy function:

db.system.js.save({
    _id:"findId", 
    value:function(){
        var doc = db.test.find({"_id": "1"})
        return doc['_id']
    }
    })

When running it, I get nothing in return.

However, if I just return db.test.find({"_id": "1"}) I do get the document.

What's the difference?

The broader question: Can I execute arbitrary JS code on server-side?

For example, I'd like to retrieve a document and have some if-else logic

0

1 Answer 1

1

db.test.find({"_id": "1"}) returns Promise, and you are not waiting for it to resolve before returning the data. So, when you return doc['_id'], doc is undefined. So, you should return the value after the Promise resolves. You can do it like this:

db.test.find({"_id": "1"}).then((document) => {
  return document .id;
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.