pythondjangomongodbmongodb-querydjango-mongodb-engine

How to use lookup operator icontains in raw query of django mongodb engine?


I am using django-mongodb-engine

Here, I used MongoDBManager to use a raw query.

class NameInfo(models.Model)
   first_name = models.CharField(max_length=50)


class Abc(models.Model):
    name = EmbeddedModelField('NameInfo')
    objects = MongoDBManager()

Abc.objects.raw_query({'name.firstname.contains':'j'})

The last line is not working as expected. So, how can I use the contains lookup type on embedded fields?

If there is another workaround not using MongoDBManager that would also be acceptable.


Solution

  • In MongoDB terms this is a $regex operation, so you issue in "raw" like this:

    Abc.objects.raw_query({ "name.firstname": { "$regex": "j" } })
    

    Note that the objects returned are not the model objects from your class definitions but simply raw python objects.