mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

Fields starting with underscore ignored on to_mongo or to_json call on DynamicDocument

Open letoss opened this issue 2 years ago • 2 comments

Hello, community.

I just realize that any field I define starting with an underscore will NOT be included in the serialization process.

I did some testing and this comes from MongoEngine since if I use the Pymongo collection, I get every field.

Is this a defined behavior? I could not find anything in the documentation about it. Please let me know if I should create an example code.

letoss avatar Jul 05 '22 07:07 letoss

Just tried in a terminal and it seems to be working ok, please provide a reproducible snippet

image

bagerard avatar Sep 06 '22 19:09 bagerard

Okay, so it looks like it doesn't work for DynamicDocuments.

In [1]: from mongoengine import DynamicDocument

In [2]: class MyDynamicDoc(DynamicDocument):
   ...:     pass
   ...: 

In [3]: MyDynamicDoc(_a="a", b="b").save()
Out[3]: <MyDynamicDoc: MyDynamicDoc object>

In [4]: MyDynamicDoc.objects.to_json()
Out[4]: '[{"_id": {"$oid": "63184cac2a33214dee46cce7"}, "b": "b"}]'

I will update the title of the issue.

letoss avatar Sep 07 '22 07:09 letoss