node.js - Query value gets quoted automatically before sending it to MongoDB? -


the following confusing me lot. have been spending quite bit of time trying understand why collection.find() doesn't work regex passed object. regex match coming on http wrapped in body of post request. try gather query (in string format) , perform query. problem seems unless regex written inside node without quotes, won't work. is, must literal without quotes.

for example, following works fine:

var query1 = {     company: {         '$regex': /goog/     } };  collection.find(query1, {}).toarray(function (err, docs) {     // got results back. awesome. }); 

however, if data comes wrapped in object, doesn't return anything. suspect it's because value gets quoted behind scenes (i.e. "/goog/"):

// assume  var query2 = {   company: {     '$regex': query.company   } };  collection.find(query2, {}).toarray(function (err, docs) {     // got nothing back. }); 

i have tested mongo shell , can confirm following:

// returns 5 results db.getcollection("contacts").find( { "company": /goog/ } )  // doesn't match db.getcollection("contacts").find( { "company": "/goog/" } ) 

furthermore, discovered following: if write value quotes

// works fine var companyregex = {'$regex': /goog/}; var query3 = {   company: companyregex }; 

so technically, "literal" regex without quotes wrapped in object works fine. if it's string, won't work. after trying replace double-quotes , single-quotes nothing (i.e. removing them.)

any idea how can regex match passed verbatim find()? i've researched it, finding lots of potential solutions, alas it's not working me.

thanks in advance!

let me focus on 1 line of post. problem might be:

the regex match coming on http wrapped in body of post request.

this seems problematic because:

the structures survive serialization between client/server are:

  • boolean
  • number
  • string
  • null *
  • objects , arrays containing these basic types
  • objects , arrays containing object , arrays [of more obj/array] of these basic types

regexp, date, function, , host of others require reconstruction, means passing string or pair of strings match , option components of regexp , running regexp() on receiving end reconstruct.

regexp gets bit messy because regexp.tostring() , regexp() not appear inverses of each others: /somematch/.tostring() "/somematch/" regexp("/somematch/") //somematch// , needed instead rebuild regexp regexp("somematch"), /somematch/. hope helps.

json.stringify(/somematch/) {} (at least on chrome).

so instead of trying build general transport, recommend re instantiating particular field regexp.

* irrelevant note: (null fine undefined peculiar. json won't stringify undefineds in objects , turns undefined null in arrays. recognize isn't part of problem, trying complete in describing can serialized.)


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -