by shigemk2

当面は技術的なことしか書かない

pymongoをごにょごにょ

まあまあこんな感じで。
平たく言えばRTFM

# easy_install pymongo
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from pymongo import Connection

#コネクション作成
con = Connection('localhost', 27017)
db = con.db_name
col = db.collection_name

# select * from col limit 1
print "========find_one========"
print col.find_one()

# 疑似desc col (mongoにdescコマンドはない)
print "========find_keys========"
for key in col.find_one():
    print key

# select * from col
print "========find_query========"
for data in col.find():
    print data

# select * from col where hoge = 1
print "========find_query========"
for data in col.find({u'hoge':11000}):
    print data

# select hoge from col where hoge = 1
print "========find_query========"
for data in col.find({u'hoge':11000},{'hoge':1}):
    print data

# select count(*) from col
print "========find_query========"
print col.find().count()

MongoDBをPythonで操作する(PyMongo使用) - Symfoware
collection – Collection level operations — PyMongo 2.6.3 documentation