【发布时间】:2014-01-17 13:01:38
【问题描述】:
这是一个具体的例子: 数据库的名称是“twitterDB”。 集合的名称是'userSets'。
userSets中的原始数据:
user_info={
"name":"britneyspears",
"password":12345,
"image":"/static/image/britney-spears-wallpaper.jpg",
"age":32,
"email":"britneyspears@example.com",
"tweets":[
{"time":"Nov. 18, 2013",
"content":"I have always been interested in the scientific discoveries underlying health advances in developing countries. The benefits of such breakthroughs are substantial, with the potential to save hundreds of thousands of lives."
},
{"time":"Nov. 1, 2013",
"content":"How cool is that? I asked him to pay off my student loans if he got me. I guess that is a no go now. ;-)"
},
{"time":"Dec. 20, 2013",
"content":"I laughed really hard at that. Those are wonderful gifts, and you are an awesome receiver! Very entertaining experience :)"
},
{"time":"Dec. 24, 2013",
"content":"That is amazing! The picture alone is worth it!!"
}
]
}
我想在 userSets 的同一文档中的“tweets”中插入以下新数据: data={"content":"这里是 xiaovid!", "time":"Dec. 30, 2013"}
这是我的实现代码:
import pymongo
from pymongo import MongoClient
conn=MongoClient()
db=conn.twitterDB
db=db.userSets
x=db.find_one()
x["tweets"].append(data)
db.save(x)
它可以工作,但我不知道为什么参数“image”的位置在插入后会发生变化。 另外,不知道有没有更正常有效的在文档中插入数据的方法。
【问题讨论】:
标签: python insert document pymongo