简体   繁体   中英

Retrieve data from a collection(mongo),modify the returned object and insert it into another mongo collection

{
        "name" : "XYZ",
        "phone" : 12345,
        "emp_id" : 9999,
        "sal" : 5000
}

I want to add data on this record and store it into another database. I have tried this:

db=connection.testing

results=db.test.find({"name":"XYZ"})

for i in results:

    i.append('dept':'Marketing')
    db.test.insert_one(i) 

(please ignore the insertion being done on the same connection)

Error:    i.append('dept':'Marketing')

                         ^
SyntaxError: invalid syntax

How to add data in the dictionary form or JSON form?

由于在您的示例中i似乎是字典,因此您很可能需要类似i.update({'dept': 'Marketing'})i['dept']='Marketing' ,以引入新的键/值对

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM