3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bson.objectidを使ってmongoDBのデータ更新をするメモ

Posted at

bottle.pyとmongoDBの学習継続中。
mongoDBのcollectionからObjectIDでdocument選択してデータを更新しようとして、エラーとの格闘に時間かかったのでメモっておく。

mongoDBはJSONと見せかけてBSON(理解できてない)。
BSONのことはbsonに任せる。

できたのはこんな感じ。

app.py

from pymongo import MongoClient
from bson.objectid import ObjectId

@app.route('/count', method='POST')
def count():

    myid = request.forms.get('myid')

    conn=pymongo.Connection()
    db=conn.mydb
    db.mycol
    mydata=db.mycol.find_one({"_id":ObjectId(myid)})
    mydata['count'] += 1
    db.mycol.save(mydata)

    return redirect('/main')

問題解決の参考はここ。
http://stackoverflow.com/questions/19731153/pymongo-find-by-id-in-subdocuments

  • 現在地点メモ

mongoDBで躓くのは大体bson絡み。
stackoverflowで調べて呪文書き写している状態。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?