def upsert_example(): """upsert的意思是: 首先尝试update, 如果找不到该文档, 则insert改文档 """ users.update({"name": "obama"}, {"$set": {"name": "obama"}}, upsert=True) fmter.tpl._straightline("after", 100) for doc in users.find({"name": "obama"}): ppt(doc)
def multiUpdate(): # users.update({}, {"name": "obama"}) # only one document are updated users.update({}, {"$set": {"name": "obama"}}, multi=True) # all document matching where clause are updated for doc in users.find(): ppt(doc) # multiUpdate()
def unset(): """移除掉某一个key, 请使用"$unset" """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$unset": {"profile": 1}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def push(): """在右边添加一项, 相当于list.append(item) """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$push": {"skillset": "data visualization"}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def unset(): """移除掉某一个key, 请使用"$unset" """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$unset": {"profile": 1}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def pop(): """在右边取出一项, 相当于list.pop() """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$pop": {"skillset": 1}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def addToSet(): """将array当成set来执行set.add(item)操作 """ users.update({"_id": 1}, {"$push": {"skillset": "python"}}) # 先加一个python, 故意添加重复项 fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$addToSet": {"skillset": "R"}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0]) # 只会在添加的时候将其当成set处理, 并不会把array自动转化为set
def pullall(): """删除多项, 相当于 for i in list, if i in [item1, item2, ...], list.remove(i) """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$pullAll": {"skillset": ["python", "cSharp", "R"]}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def pull(): """删除所有的某项, 相当于 for i in list, if i == pull_item, list.remove(i) """ users.update({"_id": 1}, {"$push": {"skillset": "python"}}) # 先加一个python, 故意添加重复项 fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$pull": {"skillset": "python"}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0]) # 把所有的python, 包括重复项都删除了
def pushAll(): """在右边加上多项, 相当于 list = list + another_list """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$pushAll": {"skillset": ["data visualization", "R"]}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def pop(): """在右边取出一项, 相当于list.pop() """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$pop": {"skillset": 1}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def push(): """在右边添加一项, 相当于list.append(item) """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$push": {"skillset": "data visualization"}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def multiUpdate(): # users.update({}, {"name": "obama"}) # only one document are updated users.update({}, {"$set": { "name": "obama" }}, multi=True) # all document matching where clause are updated for doc in users.find(): ppt(doc) # multiUpdate()
def update_without_set(): """若不使用collection.update(query, {"$set": {key: value}), 而使用: collection.update(query, new_document) 则会将所有定位到的document替换成, new_document """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"_id": 1}) # replace the whole document with the new one fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def addToSet(): """将array当成set来执行set.add(item)操作 """ users.update({"_id": 1}, {"$push": { "skillset": "python" }}) # 先加一个python, 故意添加重复项 fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$addToSet": {"skillset": "R"}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0]) # 只会在添加的时候将其当成set处理, 并不会把array自动转化为set
def pull(): """删除所有的某项, 相当于 for i in list, if i == pull_item, list.remove(i) """ users.update({"_id": 1}, {"$push": { "skillset": "python" }}) # 先加一个python, 故意添加重复项 fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$pull": {"skillset": "python"}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0]) # 把所有的python, 包括重复项都删除了
def pushAll(): """在右边加上多项, 相当于 list = list + another_list """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$pushAll": { "skillset": ["data visualization", "R"] }}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def pullall(): """删除多项, 相当于 for i in list, if i in [item1, item2, ...], list.remove(i) """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) users.update({"_id": 1}, {"$pullAll": { "skillset": ["python", "cSharp", "R"] }}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0])
def insept_example(): """insept的意思是: 首先尝试insert, 如果面临着_id重复问题, 则update 该逻辑可以用upsert实现。注: 有时候document是没有包含_id项的 """ doc = {"_id": 1, "name": "obama", "new_field": 999} try: users.insert(doc) except: _id = doc["_id"] del doc["_id"] users.update({"_id": _id}, {"$set": doc}, upsert=True) ppt(users.find({"name": "obama"})[0]) # insept_example()
def absolute_update(): """collection.update()语法分两部分, 第一部分是定位到需要修改的document, 第二部分是对值 进行设定。 注意: 使用 "$set": {key: value} 只会对key的部分进行修改, 如果使用: users.update({"_id": 1}, {"name": "obama"}), 则会将整个文档替换成 {"name": "obama"} """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) ppt(users.find({"_id": 2})[0]) users.update({"_id": 1}, {"$set": {"name": "obama", # update name field "profile.enthnicity": "african american"}}) # access child users.update({"name": "michael"}, {"age": 100}) # replace whole document, only keep _id fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0]) ppt(users.find({"_id": 2})[0])
def relative_update(): """在Sql中有 set column_name = column_name + 1 这种相对更新的方法。在mongodb中我们的做法是: 1. 使用$inc, $mul等操作符: http://docs.mongodb.org/manual/reference/operator/update-field/ 2. 首先find()找到document, 然后修改document对象, 最后再collection.save(document)保存改动。 """ fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 1})[0]) doc = users.find_one({"_id": 1}) # find the document doc["age"] += 30 # do some change to the document users.save(doc) # save changes into collections fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 1})[0]) fmter.tpl._straightline("before", 100) ppt(users.find({"_id": 2})[0]) users.update({"_id": 2}, {"$inc": {"age": 30}}) fmter.tpl._straightline("after", 100) ppt(users.find({"_id": 2})[0])