开发者

Using a loop to query in mongoDB

I'm thinking of using mongodb as a replacement for a few mysql tables that require a lot of queries (often in while/for loops) to fetch all of the data.

Mongo seems like a good fit because I can run javascript directly against the console and lay out all of the queries and receive the data back. My question is whether or not it would be faster/make more sense to do this in mongo.

My typical loops in Mysql ar开发者_开发技巧e trying to query up a tree (kind of like a file structure). For example, we start with id 6, then we query its parent which is id 5, and then its parent, etc etc until we find a parent that ends with id 0. This can take up a lot of queries and I worry that mysql will fold under this.

Sorry if I explained this poorly :P


It depends how you store the tree structure. I think the question is not really about mongo vs. mysql, but rather how you are dealing with tree storage.

It sounds like you are storing the structure as a list of tupples (id, parent_id). Instead of going back to the database for each iteration, you should get all the tupples with one query and use a simple tree building algorithm to build the structure (and filter it if you use more attributes than the tupple).

Mongo won't solve your problem, because if you store each tree node as a document, you are going to end up in the same situation you're in now.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜