Sadderdaze
Sadderdaze

Reputation: 314

Mongodb embedded document as a container for related fields

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

Upvotes: 0

Views: 85

Answers (1)

bagerard
bagerard

Reputation: 6374

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
  }
}

Upvotes: 1

Related Questions