私はmongodbの学習を始めたばかりで、まだ多くのことを知りません。
だから私のコード:
#! /usr/bin/env python2.7
import pymongo
import datetime
class AccountsDB():
def __init__(self):
self.store_info()
def store_info(self):
try:
conn = pymongo.Connection('localhost', 27017)
db_name = 'accountsdb'
coll_name = 'user_info'
db = conn[db_name]
coll = db[coll_name]
print "Successfully connected to '%s'" % db_name
for i in xrange(20):
post = {
'f_name' : 'Sergey',
'l_name' : 'Ivanov',
'number' : '777-9-777',
'user_id': i
}
coll.insert(post)
print "Done"
except:
print "Can't connect to the database"
if __name__ == "__main__":
acc = AccountsDB()
そしてそれは私に与えます:
> db.user_info.find()
{ "_id" : ObjectId("50c64872bdbff34435192a94"), "l_name" : "Ivanov", "f_name" : "Sergey", "user_id" : 0, "number" : "777-9-777" }
{ "_id" : ObjectId("50c64872bdbff34435192a95"), "l_name" : "Ivanov", "f_name" : "Sergey", "user_id" : 1, "number" : "777-9-777" }
{ "_id" : ObjectId("50c64872bdbff34435192a96"), "l_name" : "Ivanov", "f_name" : "Sergey", "user_id" : 2, "number" : "777-9-777" }
{ "_id" : ObjectId("50c64872bdbff34435192a97"), "l_name" : "Ivanov", "f_name" : "Sergey", "user_id" : 3, "number" : "777-9-777" }
{ "_id" : ObjectId("50c64872bdbff34435192a98"), "l_name" : "Ivanov", "f_name" : "Sergey", "user_id" : 4, "number" : "777-9-777" }
{ "_id" : ObjectId("50c64872bdbff34435192a99"), "l_name" : "Ivanov", "f_name" : "Sergey", "user_id" : 5, "number" : "777-9-777" }
and so on....
しかし、「_id」を取り除き、代わりに「user_id」を主キーとして使用したいと考えています。私はそれができると確信していますが、方法がわかりません。