Zac
Zac

Reputation: 109

MongoDB find method with variables

Trying to search through my mongoDB database using variable names not by specifying the exact search. Is it possible to do this? No matter how i try to format the variable I can't get a successful search. I'm trying to do something like the bottom line below

let url3 = 'http://cs544.io/11jgry2';
const ret = await dbTable.find({"shortUrl" : "http://cs544.io/11jgry2"}); //Works
const ret = await dbTable.find({"shortUrl" : url3}); //Doesn't work

Upvotes: 0

Views: 895

Answers (2)

Use $eq it Specifies equality condition. The $eq operator matches documents where the value of a field equals the specified value.

Check this this for further explanation and examples

Upvotes: 1

csurapich
csurapich

Reputation: 86

Theoretically, it should work,

But I suggest having a look into using $eq instead.

Here is an example

const ret = await dbTable.find( { shortUrl: { $eq: url3 } } );

Upvotes: 0

Related Questions