Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieval and update of embedded documents #525

Closed
mrueegg opened this issue Nov 30, 2013 · 1 comment
Closed

Retrieval and update of embedded documents #525

mrueegg opened this issue Nov 30, 2013 · 1 comment
Milestone

Comments

@mrueegg
Copy link

mrueegg commented Nov 30, 2013

Hi,

I have the following simplified data structures:

class Project(db.EmbeddedDocument):
    id = db.ObjectIdField(required=True, default=lambda: bson.ObjectId())

class Client(db.Document):
    projects = db.ListField(db.EmbeddedDocumentField(Project))

How can I retrieve the project part of a client document by a specific ID (pid)? Is it possible without doing a client-side search like follows?

client = Client.objects(projects__id=pid).first()
for p in client.projects:                                           
    if str(p.id) == project_id:
        # found project; cumbersome, what is a better solution?

With plain mongodb, I'm able to retrieve the client with only the matched project instead of getting all its projects. Is this also possible with mongoengine?

db.client.find({'projects.id': ObjectId('1238b1234c06e123f101e1c2')}, {'projects.$': true})

Then, how is it possible to update a specific project? This is what I tried but it did not work:

client.update(set__projects__S=project)

Thanks a lot!

Michael

@rozza
Copy link
Contributor

rozza commented Dec 4, 2013

Client.objects(projects__id=pid).update(set__projects__S=project) would work, you need to include it in your query.

The client.update fails as theres no query part to match against.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants