3

I have the following resource defined in settings.py,

builds = {
    'item_title': 'builds',
    'schema': {
        'sources': {
            'type': 'list',
            'schema': {
                'type': 'objectid',
                'data_relation': {
                    'resource': 'sources',
                    'embeddable': True,
                }
            }
        },
        'checkin_id': {
            'type': 'string',
            'required': True,
            'minlength': 1,
        },
    }
}

When I try to filter based on a member whose value is an objectid, I get empty list.

http://127.0.0.1:5000/builds?where={"sources":"54e328ec537d3d20bbdf2ed5"}

54e328ec537d3d20bbdf2ed5 is the id of source

Is there anyway to do this?

Yogeswaran
  • 357
  • 1
  • 13

2 Answers2

3

Your query should work just fine assuming that you actually have the 54e328ec537d3d20bbdf2ed5 value included in any sources field within any builds document.

What I mean is, you can't query the builds endpoint for the existence of a document in the sources endpoint (you can of course do that at the sources endpoint.) But, if you actually store a builds document and it references a sources document, then you query will work fine because what you are actually asking is "get me all builds documents which have a reference to this sources document". For example, if you POST a document like this to the builds endpoint:

{
    "sources": ["54e328ec537d3d20bbdf2ed5"]
    "checkin_id": "A"
}

Then this query:

http://127.0.0.1:5000/builds?where={"sources":"54e328ec537d3d20bbdf2ed5"}

Will return that one document. Of course since you defined sources as embeddable you can also do:

http://127.0.0.1:5000/builds?where={"sources":"54e328ec537d3d20bbdf2ed5"}&embedded={"sources":1}

Which will get you referenced documents embedded along with any matching document, like so:

{
    "sources": [{"field1": "hey", "field2":"I'm an embedded source"}]
    "checkin_id": "A"
}

Whereas you would get a 'raw' document without the explicit embed. It is probably worth mentioning that you can also enable predefined embedding of referenced resources, so your clients don't have to explicitly request an embed.

Hope this helps.

Nicola Iarocci
  • 6,606
  • 1
  • 20
  • 33
1

New to Eve but I have an advance on Nicola's "should work", because my experience is that it does not and as this question is what comes up when looking trying to deal with the frustration of figuring out why...

Debugging this the library got me to the point where Eve automagically decides that something with a signature that looks like "54e328ec537d3d20bbdf2ed5" should be cast to an ObjectId, which is all good. However, then the comparison of type ObjectId:54e328ec537d3d20bbdf2ed5 against type string:54e328ec537d3d20bbdf2ed5 is not an equality so your filter returns no results

The really simple solution is to change checkin_id to ObjectId. Eve starters can be assured you don't need all the additional decorations, so in the above example just change 'type':'string' to 'type':'objectId' and will be good. Specifically, if you have calling code where this field is defined as a string, you can leave it as it is, the cast will occur within eve as described above and it will just work as expected.

edit - See also eve's schema level "query_objectid_as_string" configuration setting for which upon reading seems to override this behaviour.

Dnsk
  • 51
  • 5