How to represent a property name as datetime in mongoengine class model

Viewed 155

enter image description here

How to represent a class with following data. Here i have seen fields, bp_data has nested objects, the objects are not in constant name, it represents each object with internet current datetime.

And also that bp_data objects are not array, its a document, enter image description here

Any help to represent above fig data in a mongoengine collection class.

2 Answers

MongoDB stores data in BSON, which only permits string for field name.

In order to use a date for the field name, you would have to convert it to string when storing in the database.

You could use the EmbeddedDocumentField() for bp_data to act as a separate document but is embedded to the object with the patient_id field.

For example:

class Bp_data(EmbeddedDocument):
   a_string_field = StringField()
   internet_dates = ListField(DateTimeField())

class ParentObject(Document):
   patient_id = StringField()
   bp_data = EmbeddedDocumentField(Bp_data)

Considering that you have many instances of bp_data, you should also consider EmbeddedDocumentListField() which essentially a list of EmbeddedDocuments.

[1] https://docs.mongoengine.org/apireference.html#mongoengine.EmbeddedDocument

[2] http://docs.mongoengine.org/apireference.html?highlight=datetimefield#mongoengine.fields.DateTimeField

Related