程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MongoDB數據庫 >> MongoDB綜合知識 >> MongoDB 快速入門

MongoDB 快速入門

編輯:MongoDB綜合知識

對 MongoDb 文檔數據庫感興趣,要安裝認識下。

到官方下載 win 版 http://www.mongodb.org/display/DOCS/Downloads 目前最新穩定版是 1.2.4。解壓到 f:/sofr/mongodb-win32-i386-1.2.4。

啟動 mongod
復制代碼 代碼如下:
F:\soft\mongodb-win32-i386-1.2.4>bin\mongod.exe --dbpath=f:/mongodb
Mon Mar 08 11:13:17 Mongo DB : starting : pid = 0 port = 27017 dbpath = f:/mongodb master = 0 slave
= 0 32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
** see http://blog.mongodb.org/post/137788967/32-bit-limitations for more

Mon Mar 08 11:13:18 db version v1.2.4, pdfile version 4.5
Mon Mar 08 11:13:18 git version: 5cf582d3d96b882c400c33e7670b811ccd47f477
Mon Mar 08 11:13:18 sys info: windows (5, 1, 2600, 2, 'Service Pack 3') BOOST_LIB_VERSION=1_35
Mon Mar 08 11:13:18 waiting for connections on port 27017

--dbpath 指定數據庫的目錄,默認是 /data/db,win 沒有 /data/db 目錄,所以直接雙擊 mongod 是啟動不了。默認端口是 27017

linux 下我用 legacy-static 版,因為源碼編譯沒成功。

服務器啟動後,用客戶端試用下,它自帶有個客戶端(MongoDB shell)。bin/mongo.exe
復制代碼 代碼如下:
F:\soft\mongodb-win32-i386-1.2.4>bin\mongo.exe
MongoDB shell version: 1.2.4
url: test
connecting to: test
type "exit" to exit
type "help" for help
> use test
switched to db test
> db.foo.save({a:1})
> db.foo.find()
{ "_id" : ObjectId("4b946bc03f78000000001542"), "a" : 1 }
>

mongod.exe 後端可以看到日志。
MongoDB 客戶端豐富,我用 python 試下(安裝請看:http://api.mongodb.org/python/1.4%2B/installation.html)。安裝 pymongo

復制代碼 代碼如下:
easy_install pymongo


復制代碼 代碼如下:
from pymongo import Connection
connection = Connection()
db = connection.test
for f in db.foo.find():
print "a=%s, _id=%s" % (f['a'], f['_id'])

可以看到結果。a=1.0, _id=4b946bc03f78000000001542。_id 是mongo 內部的字段。

它的像 sql 的查詢比較吸引人 ……
原文:http://blog.chenlb.com/2010/03/mongodb-quick-start.html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved