開発環境
- OS X Yosemite - Apple (OS)
- Emacs(Text Editor)
- Python 3.4 (プログラミング言語)
MongoDBイン・アクション (Kyle Banker (著)、Sky株式会社 玉川 竜司 (翻訳)、オライリージャパン)のⅡ部(MongoDB でのアプリケーション開発)、4章(ドキュメント指向データ)、4.3(データベース、コレクション、ドキュメントの基本)、4.3.2(コレクション)、キャップ付きコレクションを Python で考えてみる。
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import pymongo
import random
import time
VIEW_PRODUCT = 0
ADD_TO_CART = 1
CHECKOUT = 2
PURCHASE = 3
client = pymongo.MongoClient()
db = client['garden']
db.drop_collection('user.actions')
db.create_collection('user.actions', capped=True, size=1024)
actions = db['user.actions']
for n in range(40):
doc = dict(username='kbankder',
action_code=random.randint(0, 3),
time = time.ctime(),
n=n)
actions.insert(doc)
入出力結果(Terminal, IPython)
$ ./sample3_2.py $ mongo garden MongoDB shell version: 3.0.6 connecting to: garden Hello, you're looking particularly attractivetoday! > db.user.actions.count() 31 > db.user.actions.find() { "_id" : ObjectId("5608c9c9a54d7568c68149fe"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 9, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c68149ff"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 10, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a00"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 11, "action_code" : 2 } { "_id" : ObjectId("5608c9c9a54d7568c6814a01"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 12, "action_code" : 1 } { "_id" : ObjectId("5608c9c9a54d7568c6814a02"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 13, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a03"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 14, "action_code" : 0 } { "_id" : ObjectId("5608c9c9a54d7568c6814a04"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 15, "action_code" : 1 } { "_id" : ObjectId("5608c9c9a54d7568c6814a05"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 16, "action_code" : 0 } { "_id" : ObjectId("5608c9c9a54d7568c6814a06"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 17, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a07"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 18, "action_code" : 2 } { "_id" : ObjectId("5608c9c9a54d7568c6814a08"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 19, "action_code" : 2 } { "_id" : ObjectId("5608c9c9a54d7568c6814a09"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 20, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a0a"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 21, "action_code" : 2 } { "_id" : ObjectId("5608c9c9a54d7568c6814a0b"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 22, "action_code" : 0 } { "_id" : ObjectId("5608c9c9a54d7568c6814a0c"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 23, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a0d"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 24, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a0e"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 25, "action_code" : 2 } { "_id" : ObjectId("5608c9c9a54d7568c6814a0f"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 26, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a10"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 27, "action_code" : 2 } { "_id" : ObjectId("5608c9c9a54d7568c6814a11"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 28, "action_code" : 3 } Type "it" for more > it { "_id" : ObjectId("5608c9c9a54d7568c6814a12"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 29, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a13"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 30, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a14"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 31, "action_code" : 2 } { "_id" : ObjectId("5608c9c9a54d7568c6814a15"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 32, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a16"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 33, "action_code" : 0 } { "_id" : ObjectId("5608c9c9a54d7568c6814a17"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 34, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a18"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 35, "action_code" : 3 } { "_id" : ObjectId("5608c9c9a54d7568c6814a19"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 36, "action_code" : 2 } { "_id" : ObjectId("5608c9c9a54d7568c6814a1a"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 37, "action_code" : 0 } { "_id" : ObjectId("5608c9c9a54d7568c6814a1b"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 38, "action_code" : 1 } { "_id" : ObjectId("5608c9c9a54d7568c6814a1c"), "time" : "Mon Sep 28 14:02:01 2015", "username" : "kbankder", "n" : 39, "action_code" : 1 } > it no cursor > quit() $ # 40レコードのドキュメントを挿入、コレクション中にあるのは31レコードのドキュメント
0 コメント:
コメントを投稿