mongodb创建账号
mongodb初始化安装请参考:http://www.diyoms.com/other/163.html
MongoDB shell version: 3.2.4 MongoDB version v3.2.4
1、shell下创建数据库和账号:
账号:ceshi 密码:ceshipass
use ceshi db.createUser( { user: "ceshi", pwd: "ceshipass", roles: [ { role: "readWrite", db: "ceshi" }, { role: "readWrite", db: "test" }, ] } )
2、pymongo插入数据:
#!/usr/bin/python from pymongo.mongo_client import MongoClient client = MongoClient('mongodb://ceshi:[email protected]:27017/ceshi') db = client.test99 post_one = {"fruit": "orange"} post_many = {"fruit":{"red":"apple", "yellow":"banana"}, "device":{"computer1":"ibm", "computer2":"dell"}} result_one = db.widgets.insert_one(post_one) #插入单条数据 result_many = db.widgets.insert_many([post_many]) #插入多条数据 print db.collection_names() #查询test99库里面所有集合 print db.widgets.find_one() #查询单条数据 for i in db.widgets.find(): #查询多条数据并打印出来 print i
pymongo参考文档:http://api.mongodb.com/python/current/












