using key as value in Mongoengine

I use mongoengine for mongodb in django.

but.. mongoengine fields (like StringField) makes me build up schema toward the way that I don't want. I mean, it strictly insists that I pre-write key name before I do know what it will be. for example...

in case that I do not know what key name will be put into database...

> for(var i=0; i<10; i++){
... o = {};
... o[i.toString()] = i + 100;
... }
> db.test.find()
{ "_id" : ObjectId("4ed623aa45c8729573313811"), "0" : 100 }
{ "_id" : ObjectId("4ed623aa45c8729573313812"), "1" : 101 }
{ "_id" : ObjectId("4ed623aa45c8729573313813"), "2" : 102 }
{ "_id" : ObjectId("4ed623aa45c8729573313814"), "3" : 103 }
{ "_id" : ObjectId("4ed623aa45c8729573313815"), "4" : 104 }
{ "_id" : ObjectId("4ed623aa45c8729573313816"), "5" : 105 }
{ "_id" : ObjectId("4ed623aa45c8729573313817"), "6" : 106 }
{ "_id" : ObjectId("4ed623aa45c8729573313818"), "7" : 107 }
{ "_id" : ObjectId("4ed623aa45c8729573313819"), "8" : 108 }
{ "_id" : ObjectId("4ed623aa45c872957331381a"), "9" : 109 }


as you can see above, key is very different from each other.. just assume that "I do not know what key name will be put into document as key ahead of time

as dcrosta replied.. I am looking for a way to use mongoengine without specifying the fields ahead of time.


How can I do the same thing through mongoengine? please give me schema design like

class Test(Document):
    tag = StringField(db_field='xxxx')


I don't know what 'xxxx' will be as key name.

sorry.. I'm Korean so my english is awkward. please give me your some knowledge. Thanks for reading this.



Have you considered using PyMongo directly instead of using Mongoengine? Mongoengine is designed to declare and validate a schema for your documents, and provides many tools and conveniences around that. If your documents are going to vary, I'm not sure Mongoengine is the right choice for you.

If, however, you have some fields in common across all documents, and then each document has some set of fields specific to itself, you can use Mongoengine's DictField. The downside of this is that the keys will not be "top-level", for instance:

class UserThings(Document):
    # you can look this document up by username
    username = StringField()

    # you can store whatever you want here
    things = DictField()

dcrosta_things = UserThings(username='dcrosta')
dcrosta_things.things['foo'] = 'bar'
dcrosta_things.things['bad'] = 'quack'

Results in a MongoDB document like:

{ _id: ObjectId(...),
  _types: ["UserThings"],
  _cls: "UserThings",
  username: "dcrosta",
  things: {
    foo: "bar",
    baz: "quack"

Edit: I should also note, there's work in progress on the development branch of Mongoengine for "dynamic" documents, where attributes on the Python document instances will be saved when the model is saved. See for details and history.

Need Your Help

Subtracting hours and minutes

linux perl

I would like to subtract 17 hours and 5 minutes from 10 hours and 0 minutes.

Add marker to image - trouble converting back from relative to absolute coordinates

javascript jquery

I'm trying to add markers to an image. I don't think it's useful nor practical converting it to an image map.