1.下載數(shù)據(jù)庫http://fastdl.mongodb.org/win32/mongodb-win32-i386-1.6.5.zip
2.現(xiàn)在完成后解壓目錄,放到C盤下,然后按照如下選擇“我的電腦->屬性->高級->環(huán)境變量”,將“C:\mongodb\bin”這個路徑放到環(huán)境變量內(nèi)(如果添加環(huán)境變量可以去網(wǎng)上找).
3.選擇"開始->運行",輸入"cmd",在DOS窗口內(nèi)輸入“mongod -port 27017 -dbpath D:/deployment/mongoDb/data/db -logpath D:/deployment/mongoDb/data/log/logs.log ”,其中dbpath 為數(shù)據(jù)文件存儲路徑,logpath 為日志文件存儲路徑。
4.不要關閉上面的窗口,新打開一個DOS,輸入“mongo->use admin->db.AddUser(username,password)”,username:數(shù)據(jù)庫數(shù)據(jù)集的用戶名,password數(shù)據(jù)庫數(shù)據(jù)集操作的密碼。
5.在兩個DOS窗口內(nèi)分別按“ctrl+c”,在第一個DOS窗口內(nèi)輸入“mongod -port 27017 -dbpath D:/deployment/mongoDb/data/db -logpath D:/deployment/mongoDb/data/log/logs.log -logappend -auth”的啟動命令,auth是要求操作數(shù)據(jù)集是需要驗證。
6.在第二個DOS窗口內(nèi),輸入“mongo->use admin->db.auth(username,password)”,然后就可以對數(shù)據(jù)集進行操作(增、刪、改)。
來自:http://www.hitb.com.cn/web/guest/bbs/-/message_boards/message/26944
shell#查詢
查詢 name = "bruce" 的數(shù)據(jù)
db.users.find({ name : "bruce" });
條件操作符
$gt : >
$lt : <
$gte: >=
$lte: <=
$ne : !=、<>
$in : in
$nin: not in
$all: all
$not: 反匹配(1.3.3及以上版本)
查詢 name <> "bruce" and age >= 18 的數(shù)據(jù)
db.users.find({name: {$ne: "bruce"}, age: {$gte: 18}});
查詢 creation_date > '2010-01-01' and creation_date <= '2010-12-31' 的數(shù)據(jù)
db.users.find({creation_date:{$gt:new Date(2010,0,1), $lte:new Date(2010,11,31)});
查詢 age in (20,22,24,26) 的數(shù)據(jù)
db.users.find({age: {$in: [20,22,24,26]}});
查詢 age取模10等于0 的數(shù)據(jù)
db.users.find('this.age % 10 == 0');
或者
db.users.find({age : {$mod : [10, 0]}});
匹配所有
db.users.find({favorite_number : {$all : [6, 8]}});
可以查詢出{name: 'David', age: 26, favorite_number: [ 6, 8, 9 ] }
可以不查詢出{name: 'David', age: 26, favorite_number: [ 6, 7, 9 ] }
查詢不匹配name=B*帶頭的記錄
db.users.find({name: {$not: /^B.*/}});
查詢 age取模10不等于0 的數(shù)據(jù)
db.users.find({age : {$not: {$mod : [10, 0]}}});
#返回部分字段
選擇返回age和_id字段(_id字段總是會被返回)
db.users.find({}, {age:1});
db.users.find({}, {age:3});
db.users.find({}, {age:true});
db.users.find({ name : "bruce" }, {age:1});
0為false, 非0為true
選擇返回age、address和_id字段
db.users.find({ name : "bruce" }, {age:1, address:1});
排除返回age、address和_id字段
db.users.find({}, {age:0, address:false});
db.users.find({ name : "bruce" }, {age:0, address:false});
數(shù)組元素個數(shù)判斷
對于{name: 'David', age: 26, favorite_number: [ 6, 7, 9 ] }記錄
匹配db.users.find({favorite_number: {$size: 3}});
不匹配db.users.find({favorite_number: {$size: 2}});
$exists判斷字段是否存在
查詢所有存在name字段的記錄
db.users.find({name: {$exists: true}});
查詢所有不存在phone字段的記錄
db.users.find({phone: {$exists: false}});
$type判斷字段類型
查詢所有name字段是字符類型的
db.users.find({name: {$type: 2}});
查詢所有age字段是整型的
db.users.find({age: {$type: 16}});
對于字符字段,可以使用正則表達式
查詢以字母b或者B帶頭的所有記錄
db.users.find({name: /^b.*/i});
$elemMatch(1.3.1及以上版本)
為數(shù)組的字段中匹配其中某個元素
Javascript查詢和$where查詢
查詢 age > 18 的記錄,以下查詢都一樣
db.users.find({age: {$gt: 18}});
db.users.find({$where: "this.age > 18"});
db.users.find("this.age > 18");
f = function() {return this.age > 18} db.users.find(f);
排序sort()
以年齡升序asc
db.users.find().sort({age: 1});
以年齡降序desc
db.users.find().sort({age: -1});
限制返回記錄數(shù)量limit()
返回5條記錄
db.users.find().limit(5);
返回3條記錄并打印信息
db.users.find().limit(3).forEach(function(user) {print('my age is ' + user.age)});
結(jié)果
my age is 18
my age is 19
my age is 20
限制返回記錄的開始點skip()
從第3條記錄開始,返回5條記錄(limit 3, 5)
db.users.find().skip(3).limit(5);
查詢記錄條數(shù)count()
db.users.find().count();
db.users.find({age:18}).count();
以下返回的不是5,而是user表中所有的記錄數(shù)量
db.users.find().skip(10).limit(5).count();
如果要返回限制之后的記錄數(shù)量,要使用count(true)或者count(非0)
db.users.find().skip(10).limit(5).count(true);
分組group()
假設test表只有以下一條數(shù)據(jù)
{ domain: "www.mongodb.org"
, invoked_at: {d:"2009-11-03", t:"17:14:05"}
, response_time: 0.05
, http_action: "GET /display/DOCS/Aggregation"
}
使用group統(tǒng)計test表11月份的數(shù)據(jù)count:count(*)、total_time:sum(response_time)、 avg_time:total_time/count;
db.test.group(
{ cond: {"invoked_at.d": {$gt: "2009-11", $lt: "2009-12"}}
, key: {http_action: true}
, initial: {count: 0, total_time:0}
, reduce: function(doc, out){ out.count++; out.total_time+=doc.response_time }
, finalize: function(out){ out.avg_time = out.total_time / out.count }
} );
[
{
"http_action" : "GET /display/DOCS/Aggregation",
"count" : 1,
"total_time" : 0.05,
"avg_time" : 0.05
}
]
來自: http://hi.baidu.com/asminfo/blog/item/20301e22dcfcce50ac34de7a.html