在python中的mongoDB文档中插入json子文档 [英] Insert a json subdocument in a mongoDB document in python

查看:50
本文介绍了在python中的mongoDB文档中插入json子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 mongoDB 数据库中的文档中插入一个子文档.我想通过查找子文档进行检索,然后在那里添加一些内容.我检索文档的代码如下:

I want to insert a subdocument in a document that I have in my mongoDB database. I want to retrieve with a find a subdoc and then to add something there. My code for retrieving the document is the following:

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

如何使用res(光标)在find方法后返回的过滤后的json文件中插入子文档?我将如何将 temp_json 添加到 res 集合中?

How can I insert the subdocument in the filtered json file returned after the find method with the res (cursor)? How am I going to add the temp_json to the res collection?

EDIT:我设法通过过滤 user_id 来添加子文档.现在我想对 item_id 的情况做同样的事情.对于 user_id 的情况:

EDIT: I manage to add sub-documents by filtering the user_id. Now I want to do the same for the case of item_id. For the case of user_id:

collection.update_one({"user_id": "13245"},{"$set": {"Item.11.first_process": [], "Item.11.sec_process": []}})

对于 user_id 的情况,我如何做同样的事情,以检查 item_id 是否存在(已经这样做了),如果是,则只更新子文档:

How can I do the same for the case of user_id, to check if item_id exist (already did that) and if yes to just update the sub-sub-document:

collection.update_one({"user_id": "13245", "Item": {"11":{}}}, {"$set": {"string": "123"}}) // tried sth like that but did not work

我的整个代码如下:

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})

据我所知,您想插入多个 item_id.你可以这样做:

As I can understand your issue, you want to insert multiple item_id. You can do it like this:

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

检查插入嵌入文档的文档:设置嵌入文档中的字段

Check the documentation for inserting embeded documents: Set Fields in Embedded Documents

这篇关于在python中的mongoDB文档中插入json子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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