2

I have a querying interface for MongoDB and am sending queries from a browser to NodeJS through a HTTP request. The query is is sent as a string and can be something like this:

var query = '{_id: ObjectId(\'536b07935c89be214c000009\'), "date": ISODate("2012-12-19T06:01:17.171Z"), mail: /test/i}'

My question, how can I safely evaluate this string in order to send it to the MongoDB NodeJS client?

eval is not an option (both on MongoDB and NodeJS) as this is part of a consumer faced application.

I'm open to other safe solutions for passing a query through HTTP and properly execute them on the server.

4
  • What is the value of query in your code? Is it just a string containing the value you pasted above? How exactly does your code fail? What is the error message? You need to provide more information in order for people to help you. Commented Dec 10, 2015 at 23:53
  • @mbaird Fixed, no use to show the code I tried since it crashes and that I'm not tied to this solution Commented Dec 10, 2015 at 23:59
  • Are you using any particular MongoDB API for Node, or you don't care which one to use? Commented Dec 11, 2015 at 2:20
  • @JohnZwinck I'm currently using the official mongodb package, but I don't care which on as long as I can write standard query on the front-end and get correct results. Also, I'd like the version support to be as broad as possible Commented Dec 12, 2015 at 15:53

1 Answer 1

1
+50

The string you are presenting is a mongodb shell query. This string contains mongodb shell specific datatypes, and as such is only usable inside the mongodb shell. You can not parse or evaluate these queries in a different (javascript) enviroment, because they are not valid JSON. Hence, eval, or JSON.parse wouldn't even work, because of the specific datatypes.

If you want to serialize mongodb queries for usage in different enviroments, you could use MongoDB Extended JSON.

https://docs.mongodb.org/v3.0/reference/mongodb-extended-json/

This is standard JSON which can contain mongodb datatypes. Your query would like this in MongoDB extended JSON.

{
    "_id": {
        "$oid": "536b07935c89be214c000009"
    },
    "date": {
        "$date": "2012-12-19T06:01:17.171Z"
    },
    "mail": {
        "$regex": "test",
        "$options": "i"
    }
}

If you want to parse or evaluate a string like this to pass it along to the node.js mongodb driver, you would need to use a library to deserialize this to a proper Node.js MongoDB Driver object.

You could use this library to do that:
https://www.npmjs.com/package/mongodb-extended-json

You can also use this library in your browser to build the queries. Or you could build the mongodb queries by hand.

I'm not aware of plugin / npm package that would allow you to convert mongodb shell queries to MongoDB Extended JSON automatically. You could try to convert them automatically by implementing some of the types your self ( ISODate, ObjectId ). However you will never have a full compatibility between the mongodb shell and the mongodb nodejs driver, many methods have different signatures and return types, cursors work differently, etc...

There also is this project, an alternative to the officially supported mongodb nodejs driver, which tries to mimic the shell a bit more if you really value that, but it won't help you with your specific query, you'll still need to convert it.
https://docs.mongodb.org/ecosystem/drivers/node-js/

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

6 Comments

How can I convert a standard MongoDB query string to extended JSON? Can you write an example please? Thanks anyway
@arkihillel I Updated my answer to address your comment, let me know if you have any more questions. ( please also accept the answer if you are happy with it so I can get the bounty :D )
@willem-dhaeseleer Well, I guess I should have been more specific in my question. I want users to hook up to their DB, write usual mongodb shell queries. Then to pass them (or an equivalent like in extended JSON) through an HTTP request and execute them on server. Your solution forces users to write queries in an extended JSON fashion. I may very well accept it if what I want to do is not technically feasible
@arkihillel I think letting your users write their queries in extended mongodb json is probably the easiest thing to do here, it's well documented and human readable, it would also allows you to send the query to a python process for example. If you want to support mongodb shell queries, you will have to implement the mongodb shell datatypes and then parse the javascript and and built the appropriate query for the node.js driver.
@willem-dhaeseleer that's basically what I do now but parsing regex is (nearly?) impossible. Closing the bounty. You may want to move the can not convert part above so that it will be more straightforward for future readers
|

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.