Chinaunix首页 | 论坛 | 博客
  • 博客访问: 390903
  • 博文数量: 87
  • 博客积分: 2571
  • 博客等级: 少校
  • 技术积分: 920
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-29 13:10
文章分类

全部博文(87)

文章存档

2012年(49)

2011年(7)

2010年(26)

2009年(5)

分类: 数据库开发技术

2012-04-11 19:22:08

基本使用
1.将mongodb-win32-i386-2.0.3安装到D:\other\opensource\下
2.切换到D:\other\opensource\mongodb-win32-i386-2.0.3\bin\下
3.创建目录D:\other\opensource\mongodb-win32-i386-2.0.3\data\作为数据库目录
4.启动,并指定存储目录
mongod --dbpath D:\other\opensource\mongodb-win32-i386-2.0.3\data
默认监听27017端口
5.使用自带管理台
访问
6.用客户端访问
mongo 
> // mongo shell是连接到数据库的javascript shell 


设置为系统服务
1.切换到D:\other\opensource\mongodb-win32-i386-2.0.3\bin\下
2.设置系统服务
mongod --install --logpath D:\other\opensource\mongodb-win32-i386-2.0.3\logs\MongoDB.log --logappend --dbpath D:\other\opensource\mongodb-win32-i386-2.0.3\data --directoryperdb
(MongoDB.log文件不存在也要这么写,会自动创建)

安装python
1.到下载所需版本安装包(我选2.6:2.6/)
2.下载
3.安装(安装到C:\Python\)
4.设置环境变量
将python的安装目录C:\Python加入到系统的%path%中

执行python文件
1.用command line切换到目标文件目录
2.执行
目标文件名.py

代码示例1
打印
  1. #output.py
  2. print 1

安装mongo的python模块
1.下载pymongo-1.9.win32-py2.6.exe(对应与python的版本)
2.安装


代码示例2
连接mongodb

  1. #mongo.py
  2. #
  3. import pymongo
  4. con = pymongo.Connection('localhost', 27017)

  5. mydb = con.mydb # new a database
  6. mydb.add_user('test', 'test') # add a user
  7. mydb.authenticate('test', 'test') # check auth
  8. muser = mydb.user # new a table
  9.  
  10. muser.save({'id':1, 'name':'test'}) # add a record
  11. muser.insert({'id':2, 'name':'hello'}) # add a record
  12. muser.find_one() # find a record
  13. muser.find_one({'id':2}) # find a record by query
  14. muser.create_index('id')
  15. muser.find().sort('id', pymongo.ASCENDING) # DESCENDING

  16. # muser.drop() delete table
  17. muser.find({'id':1}).count() # get records number
  18. muser.find({'id':1}).limit(3).skip(2) # start index is 2 limit 3 records
  19. muser.remove({'id':1}) # delet records where id = 1
  20. muser.update({'id':2}, {'$set':{'name':'haha'}}) # update one recor

  21. #end of mongo.py

执行后,可以在  看到变化


阅读(3008) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

深蓝苹果2012-04-12 11:29:08

马夹GG: python 连接mysql数据库与mongoDB有什么不同? 楼主知道不? 先谢谢了.....
刚接触python,还没试过连接mysql

但应该没有区别,都是db驱动+IP:PORT

马夹GG2012-04-12 11:12:10

python 连接mysql数据库与mongoDB有什么不同? 楼主知道不? 先谢谢了