Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在数据库中插入非字符串对象作为键_Python_Mongodb_Key - Fatal编程技术网

Python 如何在数据库中插入非字符串对象作为键

Python 如何在数据库中插入非字符串对象作为键,python,mongodb,key,Python,Mongodb,Key,现在我使用的是mongoDB,并且由于数据类型限制为仅“String”,因此面临关键错误 InvalidDocument:文档必须只有字符串键,键为('good','proficious') 我尝试的行动如下: 1) 创建名为“glot”的数据库 2) 制作一个名为“usr_历史”的集合 3) 尝试将usr_历史文档插入到集合中 在jupyter笔记本中使用以下命令 import pymongo from pymongo import MongoClient client = MongoCl

现在我使用的是mongoDB,并且由于数据类型限制为仅“String”,因此面临关键错误

InvalidDocument:文档必须只有字符串键,键为('good','proficious')

我尝试的行动如下:

1) 创建名为“glot”的数据库 2) 制作一个名为“usr_历史”的集合 3) 尝试将usr_历史文档插入到集合中 在jupyter笔记本中使用以下命令

import pymongo

from pymongo import MongoClient

client = MongoClient() #to run the mongod instance we need a client to run it

db = client['glot'] #we connect to 'test-database' with the pre-defined client instance

collection = db['usr_history']

edge_attr = {('good','beneficial'):"good is beneficial", ('beneficial','supportive'):"beneficial means something supportive"} #create this with glat_algo

usr_history = {"nodes": ['good', 'beneficial', 'supportive'], "edges": [('good', 'beneficial'), ('beneficial', 'supportive')], "rephrase": edge_attr}

collection.insert_one(usr_history)
最后一个命令返回错误,如上所述

基本上,我要做的是将(顶点、边、边属性)数据存储到anyDB中,这样我就可以跟踪大量的usr\u历史数据,以便使用python进行分析

我并不局限于mongoDB,所以您可以给我一个指南或解决方案,不仅仅是mongoDB,还有其他替代方案


非常感谢。

您不能将非字符串值用作键;只有字符串可以是键

如果将
('good','beneficious')
存储为单独属性的值,效果会更好,可能如下所示:

edge\u attr=[
{“attr”:(“好的”,“有益的”),“val”:“好的是有益的”},
{“attr”:(“有益的”,“支持的”),“val”:“有益的意味着支持的东西”}
];

在MongoDB中,键是字符串格式的。例如,
“1”
是有效键,但
1
不是。我尝试在MySQL中使用“编号列”,但在通过表进行索引时,这并不是一个好兆头。因此,在任何数据库中,都要尽量避免密钥的命名约定。值可以是数字格式(
“mykey”=1
“mykey”=[1,2,3]
有效)。@Mika72 thx。从字面上说,我第一次看到这个新词bode。