2

I have a MongoDB collection of documents, each document representing a fish. Users of my application can then define a type of fish, by creating a query on the fish collection. This query can be very complex, using Conditional Operators, etc.

For example, a user could define the 'highly abnormal shark-like fish' to be any fish returned by:

{'length':{$gte : 45}, 'name' : {$in : ['Klaus', 'Alistair', 'Steve']}}

But new fish are frequently being discovered, and I need to assign types to them based on the queries created by the users. That is, I will need to use this query again many times in the future. Therefore, my thought is to have a collection with documents like this:

{'typename' : 'highly abnormal shark-like fish', 
'query' : '{'length':{$gte : 45}, 'name' : {$in : ['Klaus', 'Alistair', 'Steve']}}'}

My question is: should I store the query as a string? Is that the best way?

Please keep in mind that as new fish are discovered, I will have to apply the query using the PHP driver. Should I store the PHP-array-version of the query as a string, then use eval()?

1 Answer 1

1

If users build the queries as an input string, sure, store that. You want them to be able to edit it later. But then you already do eval, don't you?

Otherwise serialize the query document and store that.

3
  • The users build the query in a form; they don't know what's going on behind the scenes. I build the query from their form inputs, so no, I'm not doing an eval() currently.
    – Dan
    Aug 9, 2011 at 21:25
  • for eval you have to store with array(, which will look just as ugly as the serialized value, so use the latter one. Aug 9, 2011 at 21:42
  • thanks! I did not know about serialize(), it does exactly what I wanted.
    – Dan
    Aug 9, 2011 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.