ashleydavis / mongodb-rest

REST Server for MongoDB (using node.js)
GNU Lesser General Public License v3.0
75 stars 34 forks source link

Added support for nested queries on ObjectID #12

Closed nascob closed 9 years ago

nascob commented 9 years ago

This commit adds support for schemas where there are multiple attributes of ObjectID type. Query syntax is similar to the Mora-Rest API: query = {"some_attribute" : {"_id" : "550c37ebbc2e8b6a1c0031cb"}} The query will be preprocessed and the hex string will be extracted and casted to ObjectID type before being sent to mongodb for searching.

ashleydavis commented 9 years ago

Thanks for your update. Is it possible to to have objects with in objects?

eg query = {"some_attribute" : {"some_other_attribute" :{"_id" : "550c37ebbc2e8b6a1c0031cb"}}}

Do we need to process ids recursively?

nascob commented 9 years ago

I don't think so - I haven't written a recursive check to traverse down the object tree, but it would be beneficial to write that way.

ashleydavis commented 9 years ago

If you could make a recursive function that traverses the object graph and translates ids that would be awesome.

ashleydavis commented 9 years ago

Also what happens if the field is not called '_id'?

I noticed the issue that you opened that the example field is called '$oid'.

Is there anyway we can get this to work so that it translates all id fields?

Or is there another way to achieve what you want without even needing the translation?

nascob commented 9 years ago

Those are some good points. I'll give the recursive function a shot. About the naming conventions - I can write a translate function that checks for a given set of possible namings (e.g. - "$id", "$oid", "_id").

Looking at my current implementation hopefully it would be pretty straightforward to implement those things.

As a side note - currently my team has some issues with multiple filters - so after I fix them I'll get working on this.

nascob commented 9 years ago

Also - should I provide an update for the README (for the example section) once I do implement them?

ashleydavis commented 9 years ago

All sounds good. The readme is especially important... you might add a very good feature, but it's no good unless you document it to tell people about it!

nascob commented 9 years ago

The new code should now support multiple nested attributes.

I have pre-defined some names for the ObjectId in rest.js.

The only pitfall right now would be that the actual database collection (from your example) should be:

 {"some_attribute" :
    {"some_other_attribute" : "550c37ebbc2e8b6a1c0031cb"}
 }

and not:

 {"some_attribute" :
    {"some_other_attribute" :
        {"_id" : "550c37ebbc2e8b6a1c0031cb"}
    }
 }

In other words - users should not create their own MongoDB "_id" fields - it's bad practice anyways. If they do they should be able to query them, by specifying their "id" attribute as a query argument - but I have not tested that.

I will update the readme later today.

Please take a look at the code and tell me if you notice something.

nascob commented 9 years ago

@ashleydavis Have you had a chance to look at the changes?

seonpiao commented 9 years ago

This is a great feature to me ! How is the issue coming along?

nascob commented 9 years ago

I've been using this commit for several months now and it's behaving pretty well. I can guarantee that it's a 100% stable, but I'd be glad if you can test it and get back to me.

ashleydavis commented 9 years ago

Merged. Thanks for your contribution.