0

Does anyone know if there's a way to use a reference to a string in a mongo query, instead of using the string directly in it?

i.e. I want to do this:

key = "id";
value = "12345";
collection("accounts").find({key: value});

Instead of this:

collection("accounts").find({"id": "12345"});
2

1 Answer 1

1

Use the bracket notation to create the query object:

key = "_id";
value = "57ffe131b0b79719a0db75d8";
query = {};
query[key] = ObjectId(value);
collection("accounts").find(query);
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.