SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
805 stars 171 forks source link

relationship definitions and filtering on document Ids problem #15

Closed jettdigital closed 9 years ago

jettdigital commented 9 years ago

Is there something special about filtering on document ids stored in other documents? For example, imagine I have 2 objects, Author and Book.

I could setup a ONE_TO_MANY on Author like this { "role" : "INVERSE" , "type" : "ONE_TO_MANY" , "ref-field" : "author_id" , "rel" : "books" , "target-coll" : "book"}

and then a corresponding MANY_TO_ONE on Book like this { "role" : "OWNING" , "type" : "MANY_TO_ONE" , "ref-field" : "author_id" , "rel" : "author" , "target-coll" : "author"}

So when I POST in an Author and then POST in a Book setting author_id = the _id property for the Author document I want associated I get what I would expect in _links on the Author document which would be "href" : "/books?filter={'author_id':'theauthoridhere'} but for the life of me I can't get that simple filter to work with restheart created ids. I saw something in the source about detecting object ids in the CollectionDAO but I didn't really understand what it was doing.

ujibang commented 9 years ago

this duplicates issue #10, and it is fixed in the current development branch (can you confirm for your case?).

in short, the issue was that the author_id field of book was stored as a string, on the other hand the author_id in the filter expression gets interpreted as an objectid, and of course:

ObjectId('theauthoridhere') != 'theauthoridhere'

In the current development branch, not only the author_id in the filter expression but also any value in POST/PUT/PATCH json body that is a valid objectid is interpreted as an ObjectId by default (this behavior can be switched off passing the query parameter detect_oids=false).

I suggest you to use the development version and PATCH the book passing a json object with the current author_id string. This will replace the string value with the corresponding objectid instance.