首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

pymongo $addToSet 跟 $set 区别

2012-09-10 
pymongo $addToSet 和 $set 区别conn Connection(127.0.0.1,27017)db conn.coolercoll db.postspo

pymongo $addToSet 和 $set 区别

conn = Connection("127.0.0.1",27017)db = conn.coolercoll = db.postspost = {    "name":"cooler2",    "age":2,    "coo":{        "a":"a",        "c":"c"        }}#coll.remove()coll.update({"name":"cooler"},{"$addToSet":{"coo.b":"c"}})#coll.update({"name":"cooler"},post)#coll.insert(post)posts =  coll.find()for post in posts:    print post


输出结果  :

{u'age': 2, u'_id': ObjectId('4f7193237e61a170c8210195'), u'name': u'cooler', u'coo': {u'a': u'a', u'b': [{u'b': u'B'}, {u'b': u'b'}, u'c']}}
{u'age': 2, u'_id': ObjectId('4f72ba7b414ec02b84000000'), u'name': u'cooler2', u'coo': {u'a': u'd', u'x': u'a', u'c': u'c', u'k': 1}}

仔细观察:
         第一行中的  coo.b 的值 是一个【】 list
         第二行中 coo.a 这样的值 是 一个字符串

热点排行