List request in mongoengine; contains vs in

I have a ListField in the model with identifiers (ReferenceField), and I need to make a request if there is a specific identifier in this list. AFAIK I have 2 options for this:

Model.objects.filter(refs__contains='59633cad9d4bc6543aab2f39')

or

Model.objects.filter(refs__in=['59633cad9d4bc6543aab2f39'])

Which one is most effective for this use case?

The model looks like this:

class Model(mongoengine.Document):
    refs = mongoengine.ListField(mongoengine.ReferenceField(SomeOtherModel))

From what I can read in the mongoengine documentation, http://docs.mongoengine.org/guide/querying.html#string-queries contains a really string query, but it also works surprisingly here, But I assume __in more efficient since it should be optimized for lists, or am I mistaken?

+4
source share
1

, , , . - ! :

Model.objects.filter(refs__contains="5305c92956c02c3f391fcaba")._query
{'refs': ObjectId('5305c92956c02c3f391fcaba')}

.

Model.objects.filter(refs__in=["5305c92956c02c3f391fcaba"])._query
{'refs': {'$in': [ObjectId('5305c92956c02c3f391fcaba')]}}

, , , , , . refs .

+7

Source: https://habr.com/ru/post/1527762/


All Articles