0

Is it ok to use mongodb embedded document to combine related fields together?

Example: a document has fields creator_id and creator_language, is it possible to replace them with an embedded document creator, containing fields id and language without performance impact?

I haven't been able to find anything on how an embedded document is stored, except the fact that it has no collection and tied to the parent document

1 Answer 1

1

EmbeddedDocument are simply nested object inside your document. This is quite standard in mongodb and so its perfectly fine to switch to that. You may observe a performance impact with mongoengine if you start having hundreds or thousands of nested structure but it does not look like this is your plan here.

See below for the storage

class DateOfBirth(EmbeddedDocument):
    year = IntField()
    month = IntField()
    day = IntField()

class Person(Document):
    name = StringField()
    dob = EmbeddedDocumentField(DateOfBirth)

Person(name='John', dob=DateOfBirth(day=1, month=12, year=2000)).save()

will store an object like this one:

# print(Person.objects.as_pymongo().first())
{
  '_id': ObjectId('5d2decf7d8eefe0e58da364d'),
  'name': 'John',
  'dob': {
    'year': 2000,
    'month': 12,
    'day': 1
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.