MongoDB
NoSQL,基于文档的模型,高性能,比较接近关系型数据库。面向集合存储,一个集合里可存储不同类型的数据/文档。无需事先定义存储结构(schema),可存储JSON (BSON)。
安装服务端
Windows下,下载包里的mongod.exe是服务程序,命令行运行"-h"查看所有选项
指定data路径且运行服务
mongod.exe -dbpath F:\soft\mongodb\mongodb\data\db
View Code
C:\Users\localuser>F:\soft\mongodb\mongodb\bin\mongod.exe -dbpath F:\soft\mongod
b\mongodb\data
Thu May 31 11:40:29 [initandlisten] MongoDB starting : pid=4964 port=27017 dbpat
h=F:\soft\mongodb\mongodb\data 64-bit
Thu May 31 11:40:29 [initandlisten] db version v1.8.2, pdfile version 4.5
Thu May 31 11:40:29 [initandlisten] git version: 433bbaa14aaba6860da15bd4de8edf6
00f56501b
Thu May 31 11:40:29 [initandlisten] build sys info: windows (6, 1, 7600, 2, '')
BOOST_LIB_VERSION=1_42
Thu May 31 11:40:29 [initandlisten] waiting for connections on port 27017
Thu May 31 11:40:29 [websvr] web admin interface listening on port 28017
b\mongodb\data
Thu May 31 11:40:29 [initandlisten] MongoDB starting : pid=4964 port=27017 dbpat
h=F:\soft\mongodb\mongodb\data 64-bit
Thu May 31 11:40:29 [initandlisten] db version v1.8.2, pdfile version 4.5
Thu May 31 11:40:29 [initandlisten] git version: 433bbaa14aaba6860da15bd4de8edf6
00f56501b
Thu May 31 11:40:29 [initandlisten] build sys info: windows (6, 1, 7600, 2, '')
BOOST_LIB_VERSION=1_42
Thu May 31 11:40:29 [initandlisten] waiting for connections on port 27017
Thu May 31 11:40:29 [websvr] web admin interface listening on port 28017
服务运行在27017端口,一个webPage的工具可查看服务运行情况(http://host:28017/)
指定参数且安装到windows服务
mongod.exe -logpath f:\soft\mongodb\mongodb\log\mongodb.log -logappend -dbpath F:\soft\mongodb\mongodb\data -directoryperdb -serviceName MongoDB -install
客户端
mongo.exe是一个客户端程序,命令行运行,可连接到服务端
View Code
C:\Users\localuser>F:\soft\mongodb\mongodb\bin\mongo.exe 127.0.0.1
MongoDB shell version: 1.8.2
connecting to: 127.0.0.1/test
> show dbs
admin (empty)
local (empty)
twitter 0.078125GB
> use twitter
switched to db twitter
> db.twitter.insert({"name":"jack","status":"hello twitter"})
> db.twitter.find()
{ "_id" : ObjectId("4fc6eb2e4d030a5894a25613"), "name" : "jack", "status" : "hel
lo twitter" }
MongoDB shell version: 1.8.2
connecting to: 127.0.0.1/test
> show dbs
admin (empty)
local (empty)
twitter 0.078125GB
> use twitter
switched to db twitter
> db.twitter.insert({"name":"jack","status":"hello twitter"})
> db.twitter.find()
{ "_id" : ObjectId("4fc6eb2e4d030a5894a25613"), "name" : "jack", "status" : "hel
lo twitter" }
db.help()查看db的所有命令,db.collectionName.help()查看集合上的所有命令
插入数据会自动产生一个BsonObjectId属性。
SQL语句到MongoDB命令的对应关系
http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart
C# Driver
http://www.mongodb.org/display/DOCS/CSharp+Language+Center
MongoServer server = MongoServer.Create("mongodb://" + mongodbIP);
server.Connect();
MongoDatabase db = server.GetDatabase("twitter");
MongoCollection<BsonDocument> colMsg = db.GetCollection(colsNameMsg);
//查询
List<Message> items = colMsg.FindAs<Message>(Query.EQ("flag", 0)).SetSortOrder(SortBy.Ascending("_id")).SetLimit(limit).ToList<Message>();
....
server.Disconnect();
server.Connect();
MongoDatabase db = server.GetDatabase("twitter");
MongoCollection<BsonDocument> colMsg = db.GetCollection(colsNameMsg);
//查询
List<Message> items = colMsg.FindAs<Message>(Query.EQ("flag", 0)).SetSortOrder(SortBy.Ascending("_id")).SetLimit(limit).ToList<Message>();
....
server.Disconnect();