您的位置:首页 > 数据库 > Mongodb

Python对Mongodb的一些简单操作

2012-10-09 16:11 561 查看
Python很多人说好,看了些博客都说好

于是花了一天时间看了下,感觉有点matlab的样子,都是解释性的东西

注意一点细节就可以了,之后会总结下python的好处

学习Python后打算编个东西

于是想到了最近用的mongodb的操作,明显有Python的操作命令

# one file to write data set into a file from mongodb

import os

import pymongo #load
import datetime # for insert data
from pymongo import Connection

connection = Connection()
test = connection.hello.people # build the connection
#collection = test.collection()

# the next is how to index every element in the dataset
n = 1
temp = ()
for i in test.find():
temp = (temp,i["name"])
print i["name"]
#print i["age"]
#print i["city"]

#print temp
filename = file('test.txt','w')
filename.write(str(temp))
filename.close()  # write data into a txt file

#build a bson for insert
person1 = {"name": "jia"}
person2 = {"name": "yi"}
person3 = [{"name": "bing"},
{"name": "ding"}]

test.insert(person1)
test.insert(person2)
test.insert(person3)

# how to find you need
t1 = test.find_one({"name":"jia"})
print t1["name"]
t2 = test.find_one({"name":"ding"})
print t2["name"]


程序中主要包括了连接,创建元素,插入元素,修改元素因为没有用到,所以没有写

不过大家可以参考这个博客:

http://serholiu.com/python-mongodb

写的很详细。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: