0

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

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: 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": []}})

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

My whole code is the following:

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
7
  • Use the $set operator in an update method as collection.update_one({"user_id": "13245"}, {"$set": temp_json}) Commented May 3, 2017 at 8:35
  • I tried that, but the json remain the same. Nothing was added. Commented May 3, 2017 at 8:39
  • Actually it works by updating the values of the item_id. What I want to is to keep both the previous and the new one. Commented May 3, 2017 at 8:42
  • Your question is unclear. res is a cursor type object. your code should generate error. Also do you want to update the dictionary or update the document in database? Commented May 3, 2017 at 8:45
  • Try collection.update_one({"user_id": "13245"}, {"$set": { "item_id.111111.first_process": [], "item_id.111111.sec_process": [] }}) Commented May 3, 2017 at 8:45

1 Answer 1

4

Try this:

collection.update_one({"user_id": "13245"},{"$set": temp_json})

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

Sign up to request clarification or add additional context in comments.

11 Comments

This solution just updates my document with the last item_id that I am using. I want to keep track all the previous temp_json inside my database.
I tried to use instead of update_one insert but then it creates a new document from scratch for the same user_id.
there are multiple item_id for a single user_id?
Yes exactly. This is what I am trying to achieve.
With the use of insert instead of update_one i managed to create two docs with the same user_id. I want to have one doc for a unique user_id and for every different item_id a subdoc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.