在python中的mongoDB文档中插入json子文档

在python中的mongoDB文档中插入json子文档,python,json,mongodb,Python,Json,Mongodb,我想在mongoDB数据库中的文档中插入一个子文档。我想用一个搜索子文档进行检索,然后在那里添加一些内容。我检索文档的代码如下所示: db = client['database'] col= db['database_values'] res = col.find({"user_id": "13245"}) //returns the document after user_id which begins with item_id temp = {"item_id": {"11": {"fir

我想在mongoDB数据库中的文档中插入一个子文档。我想用一个搜索子文档进行检索,然后在那里添加一些内容。我检索文档的代码如下所示:

db = client['database']
col= db['database_values']
res = col.find({"user_id": "13245"}) //returns the document after user_id which begins with item_id

temp = {"item_id": {"11": {"first_process": [],"sec_process": [],}}}
res.update(temp)  //this is not working
from pymongo import MongoClient
collection = db['database']
res = collection.find({"User": "123"}) 
if res.count() == 0:
   print "zero"
   dss_historical_json = {"User": "123", "Item": {"123456": {"process1": [],"process2": [],}}}
   process1_json = {"timestamp": "21354879546213", "process1_value": 0.4, "state": {"B": 0.1, "F": 0.2, "E": 0.3}}
   process2_json = {"timestamp": "11354879546213", "process2_value": 0.2, "performance": 0.8}
   dss_historical_json["Item"][str(123456)]["process1"].append(process1_json)
   dss_historical_json["Item"][str(123456)]["process2"].append(process2_json)
   temp = db.historical_values
   temp = db.historical_values
   post_id = temp.insert_one(dss_historical_json).inserted_id
else:
   for line in res:
      counter = 0
      for key in line["Item"].keys():
         if line["Item"].keys()[counter] == "123469":
         collection.update_one({"User": "123"}, {"$addToSet": {"Item.123469.process1": [{"timestamp": "213546879", "process1_value": 1,"state": {"B": 0.1, "F": 0.2, "E": 0.3}}],"Item.123469.process2": [{"timestamp": "11354879546213","process2_value": 0.2,"performance": 0.8}]}})
         else:
             collection.update_one({"User": "123"},{"$set": {"Item.123469.process1": [{"timestamp": "213546879", "process1_value": 1, "state": {"B": 0.1, "F": 0.2, "E": 0.3}}],
                                                       "Item.123469.process2": [{"timestamp": "11354879546213", "process2_value": 0.2, "performance": 0.8}]}})
         counter = counter + 1
如何使用res(游标)在find方法之后返回的过滤json文件中插入子文档?如何将temp_json添加到res集合

编辑:我通过过滤用户标识来添加子文档。现在我想对项目标识执行相同操作。对于用户标识:

collection.update_one({"user_id": "13245"},{"$set": {"Item.11.first_process": [], "Item.11.sec_process": []}})
对于用户_id的情况,如何执行相同的操作,以检查项目_id是否存在(已经存在),如果是,则仅更新子文档:

collection.update_one({"user_id": "13245", "Item": {"11":{}}}, {"$set": {"string": "123"}}) // tried sth like that but did not work
我的全部代码如下:

db = client['database']
col= db['database_values']
res = col.find({"user_id": "13245"}) //returns the document after user_id which begins with item_id

temp = {"item_id": {"11": {"first_process": [],"sec_process": [],}}}
res.update(temp)  //this is not working
from pymongo import MongoClient
collection = db['database']
res = collection.find({"User": "123"}) 
if res.count() == 0:
   print "zero"
   dss_historical_json = {"User": "123", "Item": {"123456": {"process1": [],"process2": [],}}}
   process1_json = {"timestamp": "21354879546213", "process1_value": 0.4, "state": {"B": 0.1, "F": 0.2, "E": 0.3}}
   process2_json = {"timestamp": "11354879546213", "process2_value": 0.2, "performance": 0.8}
   dss_historical_json["Item"][str(123456)]["process1"].append(process1_json)
   dss_historical_json["Item"][str(123456)]["process2"].append(process2_json)
   temp = db.historical_values
   temp = db.historical_values
   post_id = temp.insert_one(dss_historical_json).inserted_id
else:
   for line in res:
      counter = 0
      for key in line["Item"].keys():
         if line["Item"].keys()[counter] == "123469":
         collection.update_one({"User": "123"}, {"$addToSet": {"Item.123469.process1": [{"timestamp": "213546879", "process1_value": 1,"state": {"B": 0.1, "F": 0.2, "E": 0.3}}],"Item.123469.process2": [{"timestamp": "11354879546213","process2_value": 0.2,"performance": 0.8}]}})
         else:
             collection.update_one({"User": "123"},{"$set": {"Item.123469.process1": [{"timestamp": "213546879", "process1_value": 1, "state": {"B": 0.1, "F": 0.2, "E": 0.3}}],
                                                       "Item.123469.process2": [{"timestamp": "11354879546213", "process2_value": 0.2, "performance": 0.8}]}})
         counter = counter + 1
试试这个:

collection.update_one({"user_id": "13245"},{"$set": temp_json})
我可以理解您的问题,您希望插入多个
项目\u id
。您可以这样做:

collection.update_one({"user_id": "13245"},{"$set": {"item_id.111111":temp_json.get('item_id').get('111111')}})
检查文档以插入嵌入的文档:

尝试以下操作:

collection.update_one({"user_id": "13245"},{"$set": temp_json})
我可以理解您的问题,您希望插入多个
项目\u id
。您可以这样做:

collection.update_one({"user_id": "13245"},{"$set": {"item_id.111111":temp_json.get('item_id').get('111111')}})


检查插入嵌入文档的文档:

在更新方法中使用
$set
操作符作为
collection.update_one({“user\u id”:“13245”},{“$set”:temp\u json})
我尝试过,但json保持不变。没有添加任何内容。实际上,它是通过更新项目id的值来工作的。我想保留上一个和新的。您的问题不清楚
res
是一个
光标
类型对象。您的代码应该会生成错误。您还想更新字典还是更新数据库中的文档?请尝试
collection.update_one({“user_id”:“13245”},{“$set”:{“item_id.111111.first_process”:[],“item_id.111111.sec_process”:[]})
在更新方法中使用
$set
操作符作为
collection.update_one({“user_id”:“13245”{“$set”:temp_})
我试过了,但json还是一样。没有添加任何内容。实际上,它是通过更新项目id的值来工作的。我想保留上一个和新的。您的问题不清楚
res
是一个
光标
类型对象。您的代码应该会生成错误。是否还要更新字典或更新数据库中的文档?请尝试
collection.update_one({“user_id”:“13245”},{“$set”:{“item_id.111111.first_process”:[],“item_id.111111.sec_process”:[]})
此解决方案仅使用我正在使用的最后一个item_id更新我的文档。我想跟踪数据库中以前的所有临时json。我尝试使用一个插入而不是更新一个插入,但它会从头开始为同一个用户id创建一个新文档。对于单个用户id,有多个
项id
?是的。这就是我试图实现的目标。通过使用insert而不是update\u one,我成功地创建了两个具有相同用户id的文档。我希望为唯一用户id创建一个文档,并为每个不同的项目id创建一个子文档。此解决方案仅使用我正在使用的最后一个项目id更新我的文档。我想跟踪数据库中以前的所有临时json。我尝试使用一个插入而不是更新一个插入,但它会从头开始为同一个用户id创建一个新文档。对于单个用户id,有多个
项id
?是的。这就是我要实现的。使用insert而不是更新一个,我成功地创建了两个具有相同用户id的文档。我希望为唯一用户id和每个不同的项目创建一个文档,一个子文档。