在Mongoengine中使用键作为值 [英] using key as value in Mongoengine

查看:104
本文介绍了在Mongoengine中使用键作为值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是,mongoengine字段(如StringField)使我可以按照我不想要的方式构建模式。我的意思是,它严格地坚持在我知道它将是什么之前,我预先写下密钥名称。例如...



如果我不知道什么密钥名称将被放入数据库...

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

[添加]



如上图所示,键是非常不同的。
只是假设我不知道什么密钥名称将被提交到文档中作为关键提前



作为dcrosta回复..我正在寻找一种方法来使用mongoengine而不指定字段a



[/ add]



我如何通过mongoengine做同样的事情?
请给我的架构设计,如

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

[添加]



我不知道什么'xxxx'将作为关键的名字。



抱歉..我是韩国人,所以我的英语很尴尬。
请给我你的一些知识。
感谢您的阅读。



[/ addition]

解决方案

你是否考虑直接使用PyMongo而不是使用Mongoengine? Mongoengine旨在为您的文档声明和验证模式,并提供了许多工具和便利。如果您的文件有所不同,我不确定Mongoengine是您的正确选择。



但是,如果所有文档中都有一些共同的字段,然后每个文档都有一些特定于本身的字段,可以使用Mongoengine的 DictField 。其缺点是键不会是顶级,例如:

  class UserThings(Document): 
#你可以通过用户名查看这个文件
username = StringField()

#你可以在这里存储任何你想要的
things = DictField()

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

结果是一个MongoDB文件,如:

  {_id:ObjectId(...),
_types:[UserThings],
_cls:UserThings,
用户名:dcrosta,
东西:{
foo:bar,
baz:quack
}
}

编辑:我还应该注意到,Mongoengine的开发部门正在进行动态文件,其中Python文档实例上的属性将会保存模型时保存。有关详细信息,请参阅 https://github.com/hmarr/mongoengine/pull/112 历史。


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.save(o)
... }
> 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 }

[addition]

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.

[/addition]

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

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

[addition]

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.

[/addition]

解决方案

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'
dcrosta_things.save()

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 https://github.com/hmarr/mongoengine/pull/112 for details and history.

这篇关于在Mongoengine中使用键作为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆