python mongodb测试

1.下载pymongo-1.11.win32-py2.6.exe,因python为2.6,安装
2.下载mongodb-win32-i386-1.8.1解压
3.cmd下运行:
   J:\soft\mongodb-win32-i386-1.8.1\mongodb-win32-i386-1.8.1\bin\mongod --dbpath=J:\soft\mongodb-win32-i386-1.8.1\mongodb-win32-i386-1.8.1\data\db
  看到,最后有listening on port 28107,就正常启动服务了
4.新起cmd窗口J:\soft\mongodb-win32-i386-1.8.1\mongodb-win32-i386-1.8.1\bin\mongo.exe
  显示连接,信息与版本就正常了
5.python测试脚本
  
#! /usr/bin/env python
#coding=utf-8
from pymongo import Connection  
import datetime  
class MongoDBTest:  
      
    def __init__(self):  
        self.ConnectionToDB()  
          
    def ConnectionToDB(self):  
        print "Connection to db svr"  
        self.conn = Connection('localhost', 27017)  
        self.db = self.conn['test']  
        self.db.authenticate("a", "1")    
          
    def SaveADoc(self):  
        """ 
            保存文档 
        """  
        print "SaveDocs..."  
        #Documnt  
        post = {"author": "Mike",  
                "text": "My first blog post!",  
                "tags": ["mongodb", "python", "pymongo"],  
                "date": datetime.datetime.utcnow()  
        }  
        #insert a document into a collection  
        posts = self.db.posts  
        posts.insert(post)  
          
    def InsertBulk(self):  
        """ 
        批量插入 
        """  
        print "SaveDocs..."  
        new_posts = [{"author": "Mike",  
               "text": "Another post!",  
               "tags": ["bulk", "insert"],  
               "date": datetime.datetime(2009, 11, 12, 11, 14)},  
              {"author": "Eliot",  
               "title": "MongoDB is fun",  
               "text": "and pretty easy too!",  
               "date": datetime.datetime(2009, 11, 10, 10, 45)}]  
        posts = self.db.posts  
        posts.insert(new_posts)  
          
    def GetCollectionNames(self):  
        """ 
        取当前数据库中的所有Collection名 
        """  
        print "Collections:"  
        print self.db.collection_names()  
          
    def GetSingleDoc(self, arg):  
        """ 
        获取第一个匹配的Document对象,没有结果时将返回None 
        """  
        print "First matching:"  
        print self.db.posts.find_one(arg);  
          
    def QueryDoc(self, arg):  
        """ 
        查询多个匹配的Document 
        """  
        print "Query condition:" , arg  
        posts = self.db.posts  
        for post in posts.find(arg):  
            print ">>>>>>",post  
        print "Find count:", posts.find(arg).count()  
          
    def RangeQueries(self, arg1, arg2):  
        """ 
        范围查询,并使用arg2排序 
        """  
        print "Arg 1:", arg1  
        print "Arg 2:", arg2  
        posts = self.db.posts  
        for post in posts.find({"date": {"$lt": arg1}}).sort(arg2):  
            print post  
          
m = MongoDBTest()  
m.SaveADoc();  
m.InsertBulk()  
m.GetCollectionNames()  
#不用查询参数  
m.GetSingleDoc(None)  
#使用查询参数  
m.GetSingleDoc({"author" : "Eliot"})  
#查询多个返回值  
m.QueryDoc({"author" : "Mike"})  
#查询并排序  
arg1 = datetime.datetime(2009, 11, 12 ,12)  
arg2 = "author"  
m.RangeQueries(arg1, arg2)  

猜你喜欢

转载自tofhao.iteye.com/blog/1112224