1

I have this string:

var filter = '{stamps:{$gte: 2020-11-06 06:42:25.000+01:00, $lte: 2020-11-06 09:52:25.000+01:00}'

Somehow I have to query mongoDB collection with this string.

Message.find(filter, function(err, messages){
  if(err){
    console.log(err);
  }else{
    //do something here
  }
})

Of course I am getting this error:

ObjectParameterError: Parameter "filter" to find() must be an object, got {stamps:{$gte: 2020-11-06 06:42:25.000+01:00, $lte: 2020-11-06 09:52:25.000+01:00}

is there some way to cast this string to object that will be acceptable to mongoDB?

4
  • What about JSON.parse(filter)? Commented Nov 24, 2020 at 14:56
  • WHat is filter? Mongooose document? Commented Nov 24, 2020 at 15:07
  • after JSON.parse(filter) i am getting this error - SyntaxError: Unexpected token s in JSON at position 1 at JSON.parse (<anonymous>) Commented Nov 24, 2020 at 15:22
  • @Richard Rublev filter is a string I am getting trough URL Commented Nov 24, 2020 at 15:49

1 Answer 1

1

You could either change the way you construct your query, to pass it as JSON an then use JSON.parse() / JSON.stringify()

// Note the necessary double quotes around the keys
const jsonFilter = '{ "stamps": { "$gte": "2020-11-06 06:42:25.000+01:00", "$lte": "2020-11-06 09:52:25.000+01:00" } }'
const query = JSON.parse(jsonFilter)
Message.find(query, callback)

Or you could introduce another dependency like mongodb-query-parser that can parse the string for you.

const parser = require('mongodb-query-parser')
// Note it still needs double quotes around the dates to run properly
const filter = '{stamps:{$gte: "2020-11-06 06:42:25.000+01:00", $lte: "2020-11-06 09:52:25.000+01:00" } }'
const query = parser(filter)
Message.find(query, callback)

https://runkit.com/5fbd473b95d0a9001a2359b3/5fbd473b98492c001a8bba06


If you can't include the quotes around the values in the string, you can use a regular expression to add them. But keep in mind that this regex is tailored to match this exact date format. If the format can change or you want to also match other data types, you should try to include them in your filter string from the start.

const parser = require("mongodb-query-parser")
const filter = '{stamps:{$gte: 2020-11-06 06:42:25.000+01:00, $lte: 2020-11-06 09:52:25.000+01:00} }'
const filterWithQuotedDates = filter.replace(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}(\+\d{2}:\d{2})?)/g, '"$1"')
parser(filterWithQuotedDates)
Message.find(query, callback)

https://runkit.com/5fbd494bcd812c0019b491fb/5fbd4951ebe43f001a5b590a


It should be noted that a common use-case is to pass a MongoDB query via URL params and that there are special packages for that:

Sign up to request clarification or add additional context in comments.

5 Comments

i tried mongodb-query-parser but i am getting this error - SyntaxError: Unexpected token (1:27)
@Benno I am also getting the same error,can you reproduce the code?
@VladimirSarac the problem is the missing quotes around the dates. Do you have the possibility to include them? Otherwise they can be added e.g. via a Regex.. runkit.com/5fbd473b95d0a9001a2359b3/5fbd473b98492c001a8bba06
@RichardRublev here's a version that adds quotes around the dates with a simple regular expression runkit.com/5fbd494bcd812c0019b491fb/5fbd4951ebe43f001a5b590a
@VladimirSarac Glad I could help. Please consider marking my answer as accepted if it solved your problem

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.