Question

Let's say I have this:

class Parent(models.Model):
    id = models.IntegerField(primary_key=True)
    children = ListField(EmbeddedModelField('Child'))

class Child(models.Model):
    id = models.IntegerField(primary_key=True)

In the mongo interactive shell, finding Parent's with a particular Child is as easy as:

db.myapp_parent.find({'children.id': 123})

How is this done in django-nonrel?

I tried a few things including I looked for raw queries but raw_results is not a method in Parent.objects for some reason.

FWIW, this is what I have in my requirements.txt:

git+https://github.com/django-nonrel/django@nonrel-1.3
git+https://github.com/django-nonrel/djangotoolbox@toolbox-1.3
git+https://github.com/django-nonrel/mongodb-engine@mongodb-engine-1.3
Was it helpful?

Solution

I think I found the answer myself:

https://groups.google.com/forum/#!topic/django-non-relational/kCLOcI7nHS0

Basically, looks like this is not supported yet.

So the workaround is raw queries.

In order to make raw queries the code in the question should be modified to:

from django_mongodb_engine.contrib import MongoDBManager


class Parent(models.Model):
    id = models.IntegerField(primary_key=True)
    children = ListField(EmbeddedModelField('Child'))

    objects = MongoDBManager()


class Child(models.Model):
    id = models.IntegerField(primary_key=True)

Then

Parent.objects.raw_query({'children.id': 123})

works.

OTHER TIPS

Looked around for a while and suddenly the following mentioned there worked like magic for me, that appears to avoid the need for raw queries (adapted to your example):

from django_mongodb_engine.query import A

...

Parent.objects.filter( children = A('id', '123') )

As for requirements:

git+https://github.com/django-nonrel/django@nonrel-1.5
git+https://github.com/django-nonrel/djangotoolbox@toolbox-1.8
git+https://github.com/django-nonrel/mongodb-engine
#(django-mongodb-engine==0.6.0)
#(pymongo==3.2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top