Thursday, 27 April 2017

How to use a variable inside a MongoDB query (node js)

I have a database of films and would like to be able to query it with a string variable, but am having problems.

The first thing I tried:

var query = {'FilmName': filmTitle } //filmTitle is a variable
            collection.find(query).forEach( function(myDoc) { 
                console.log( "Words: " + myDoc.Words ); 
                //to print some contents of the document
            } )

From what I've gathered I might need to use regex or build the query in a different way but can't figure out how to do this. Any help would be greatly appreciated.



via rish1603

No comments:

Post a Comment