可以使用数字键的 mongoengine 吗?
Is mongoengine with numeric keys possible?
我有 mongo- 具有这样结构的文档
{
"_id":{
"$oid":"5ea6c61c8a94f7c75e426669"
},
"title":"The title",
"attributes":{
"5e733c347e6caf0a4d643e7f":[
"5e7b1e62bf924d3a5ed181de"
],
"5e733c6c7e6caf0a4d6443c8":[
"1"
],
"5e73697aabee30028e573c4d":[
"5e7cc610654336666131866f"
]
},
"id":"5e7fc2426a499664e15de014"
}
是否可以使用 mongoengine 映射像“5e73697aabee30038e573c0d”这样的字段名称?
这些变体不起作用
class attributes(EmbeddedDocument):
5e733c347e6caf0a4d643e7f = list()
class attributes(EmbeddedDocument):
'5e733c347e6caf0a4d643e7f' = list()
我找到了适合我的解决方案。我必须使用 db_field 参数:
class attributes(EmbeddedDocument):
field_name = ListField(db_field='5e733c347e6caf0a4d643e7f')
我找到了适合我的解决方案。我必须使用 db_field 参数:
class attributes(EmbeddedDocument):
field_name = ListField(db_field='5e733c347e6caf0a4d643e7f')
您能否详细说明 mongo 文档的结构。在我看来,您正在使用 mongoid 作为值。有一种巧妙的方法可以做到这一点。您可以使用参考字段。
示例:
class user(Document):
#define all the values you want here
class attributes(Document):
attrib=ReferenceField("user")
当您在集合中有很长的嵌入式文档列表时,这种类型的结构会有所帮助,并且可以更轻松地在数据库中搜索和执行其他工作。
我有 mongo- 具有这样结构的文档
{
"_id":{
"$oid":"5ea6c61c8a94f7c75e426669"
},
"title":"The title",
"attributes":{
"5e733c347e6caf0a4d643e7f":[
"5e7b1e62bf924d3a5ed181de"
],
"5e733c6c7e6caf0a4d6443c8":[
"1"
],
"5e73697aabee30028e573c4d":[
"5e7cc610654336666131866f"
]
},
"id":"5e7fc2426a499664e15de014"
}
是否可以使用 mongoengine 映射像“5e73697aabee30038e573c0d”这样的字段名称? 这些变体不起作用
class attributes(EmbeddedDocument):
5e733c347e6caf0a4d643e7f = list()
class attributes(EmbeddedDocument):
'5e733c347e6caf0a4d643e7f' = list()
我找到了适合我的解决方案。我必须使用 db_field 参数:
class attributes(EmbeddedDocument):
field_name = ListField(db_field='5e733c347e6caf0a4d643e7f')
我找到了适合我的解决方案。我必须使用 db_field 参数:
class attributes(EmbeddedDocument):
field_name = ListField(db_field='5e733c347e6caf0a4d643e7f')
您能否详细说明 mongo 文档的结构。在我看来,您正在使用 mongoid 作为值。有一种巧妙的方法可以做到这一点。您可以使用参考字段。 示例:
class user(Document):
#define all the values you want here
class attributes(Document):
attrib=ReferenceField("user")
当您在集合中有很长的嵌入式文档列表时,这种类型的结构会有所帮助,并且可以更轻松地在数据库中搜索和执行其他工作。