开发者

Storing a (complex) MongoDB query in MongoDB

开发者 https://www.devze.com 2023-03-27 11:47 出处:网络
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 c

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()?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消