pymongo - mongodb中基于位置的数组修改
淡淡烟草味
淡淡烟草味 2017-04-28 09:06:32
0
1
690
**>db.blog.findOne()
{    "_id":"djsofjwo"
    "comments":[
        {
            "comment":"good post",
            "author":"john"
            "vote":0}
            
        {
            "comment":"I thought it is too short"
            "author":"bob"
            "vote":3
        }
        
        {
            "comment":"good"
            "author":"light"
            "vote":1
        }
      ]
   }
>db.blog.update({"post":post_id},{"$inc":{"comments.0.vote":1}})

这样是在shell 的环境下对comments数组中位置为0的评论修改,使投票加一。在pymongo下怎么用python代码实现同样的功能?**

淡淡烟草味
淡淡烟草味

membalas semua(1)
黄舟
//可以用update_one
from pymongo import MongoClient
client = MongoClient()
db = client.test
result=db.restaurants.update_one({"post":post_id},{"$inc":{"comments.0.vote":1}})
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan