Saving and updating nested documents with MongoEngine

Gaetano9704 Di Benedetto

I want to implement this structural model to store my data on Mongodb with MongoEngine on flask:

skills = [{"asm":"Assembly",
           "flag":False,
          "date": datetime},
          {"java":"Java",
           "flag":False,
           "date": datetime}]

So I don't know how I can declare and update this kind of structure. For updating one object I used:

User.objects(skills=form.skills.data).update_one()

However, I don't know how to update more fields in one shot. I tried with the code below but it doesn’t work.

now = datetime.now()
 User.objects(skills=form.skills).update_one(set__skills = ({'ruby':'Ruby'}, {'flag':'true'},{'date':now}))

What kind of fields should I declare on forms.py?

rulezzer

For what I understood, you need a a nested document (skills) into another (who refers to User in this case). For doing something like this you don't have to update atomically a field but append values to the subdocument and the save everything. Tryin' to follow your example, in your case should do something like this:

user = User.objects(email=current_user.email).get()

To get the BaseQuery that refers to user X through a certain query filter, in my example the email of the current logged user

user.kskills.append(SubDocumentClass(skillName="name_of_the_skill", status=True, date=datetime.now()))

For append a collection to the subdocument list. (I've appended your field)

user.save() 

To save everything

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Elastic - prevent updating documents

分類Dev

Django: Object attribute not saving/updating

分類Dev

Sorting documents by a nested field

分類Dev

Exclude nested documents based on a condition

分類Dev

Ecto - Updating nested (Polymorphic) Associations

分類Dev

Attributes not saving with devise and accepts_nested_attributes_for

分類Dev

How to get keys of nested mongodb documents

分類Dev

Sort Documents based on nested child count in ElasticSearch

分類Dev

Persisting DateTime when saving/updating entities in fluent nhibernate

分類Dev

Upsert issue when updating multiple documents using an array of IDs with $in

分類Dev

Updating existing documents in ElasticSearch (ES) while using rollover API

分類Dev

Updating all documents in a collection (million+) with Date object

分類Dev

Nested components in re-frame not updating

分類Dev

Updating a nested record with new data in elm

分類Dev

Data Binding Not Updating w/ Nested ngFor Loops

分類Dev

Saving nested json object path to a variable via recursion

分類Dev

How to read attributes out of multiple nested documents in MongoDB Java?

分類Dev

Python: Updating Value for Nested Dictionary Inside a Class Function

分類Dev

Python: Updating Value for Nested Dictionary Inside a Class Function

分類Dev

Nested templates failed when updating from knockout 2.3 to 3.2

分類Dev

Get all documents from mongo collection using a nested list comprehension in Python

分類Dev

Get all documents from mongo collection using a nested list comprehension in Python

分類Dev

mongoengine using Authentication Database

分類Dev

Mongoengine query with date range

分類Dev

Classical mapping in MongoEngine

分類Dev

Order_by length of listfield in mongoengine

分類Dev

MongoEngineの「reload()」とは

分類Dev

mongoengineのEmbeddedDocumentFieldとReferenceField

分類Dev

PyMongo / mongodumpに相当するMongoengine

Related 関連記事

  1. 1

    Elastic - prevent updating documents

  2. 2

    Django: Object attribute not saving/updating

  3. 3

    Sorting documents by a nested field

  4. 4

    Exclude nested documents based on a condition

  5. 5

    Ecto - Updating nested (Polymorphic) Associations

  6. 6

    Attributes not saving with devise and accepts_nested_attributes_for

  7. 7

    How to get keys of nested mongodb documents

  8. 8

    Sort Documents based on nested child count in ElasticSearch

  9. 9

    Persisting DateTime when saving/updating entities in fluent nhibernate

  10. 10

    Upsert issue when updating multiple documents using an array of IDs with $in

  11. 11

    Updating existing documents in ElasticSearch (ES) while using rollover API

  12. 12

    Updating all documents in a collection (million+) with Date object

  13. 13

    Nested components in re-frame not updating

  14. 14

    Updating a nested record with new data in elm

  15. 15

    Data Binding Not Updating w/ Nested ngFor Loops

  16. 16

    Saving nested json object path to a variable via recursion

  17. 17

    How to read attributes out of multiple nested documents in MongoDB Java?

  18. 18

    Python: Updating Value for Nested Dictionary Inside a Class Function

  19. 19

    Python: Updating Value for Nested Dictionary Inside a Class Function

  20. 20

    Nested templates failed when updating from knockout 2.3 to 3.2

  21. 21

    Get all documents from mongo collection using a nested list comprehension in Python

  22. 22

    Get all documents from mongo collection using a nested list comprehension in Python

  23. 23

    mongoengine using Authentication Database

  24. 24

    Mongoengine query with date range

  25. 25

    Classical mapping in MongoEngine

  26. 26

    Order_by length of listfield in mongoengine

  27. 27

    MongoEngineの「reload()」とは

  28. 28

    mongoengineのEmbeddedDocumentFieldとReferenceField

  29. 29

    PyMongo / mongodumpに相当するMongoengine

ホットタグ

アーカイブ