Python对Mongodb的一些简单操作

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

写的很详细。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值