標籤:des style blog http io os 使用 java ar
mongodb查詢find(2012-01-05 11:38:10)
轉載▼
mongodb查詢find
指定返回的鍵,通過find或findOne的第二個參數指定想要的鍵。
> db.visithomepage.find({},{"user_id" : 1,"time" :1})
{ "_id" : ObjectId("4e0713be3ded0ab30ccb4b81"), "user_id" : NumberLong(22715364), "time" : NumberLong(1305108928) }
把不需要看到的,改成0,比如把_id改成0,不讓他回複。
> db.visithomepage.find({},{"user_id" : 1,"time" :1,"_id" : 0}) { "user_id" : NumberLong(23020418), "time" : NumberLong(1305105014) }
限制:不可以引用文檔中其他鍵的值。
--------------------------------------------------------------------------------
查詢條件:
$lt,$lte,$gt,$gte,$ne < <= > >= ≠ > db.visithomepage.find({"user_id" : { "$lte" : 23011003,"$gte" : 23010000}})
尋找日期 start = new Date("01/01/2007") db.visithomepage.find({registered" : { "$lt" : start }})
Or查詢 $in 和 $or > db.visithomepage.find({"user_id" : { "$in" : [23011003, 23010000]}})
當然還有$nin
$in能對單個鍵做查詢。要是對多個鍵做查詢,就要用$or了,$or的最佳化在於第一個條件竟可能的匹配更多的文檔。
> db.visithomepage.find({"$or" : [{"user_id" : 23011003}, {"time" : 1305115967}]})
$nin 不包含 查詢age的值在7,8 範圍外的資料 db.c1.find({age:{$nin: [7,8]}});
--------------------------------------------------------------------------------
$not
他是元條件,可以用在任何其他條件之上。
例如$mod模數運算子來說, db.users.find({"user_id" : {"$mod" : [5, 1]}}) 上面的查詢會返回1,6,11,16等使用者。要是想返回2,3,4,5,7,8,9,10,12等使用者就要用$not了 db.users.find({"user_id" : {"$not" :{"$mod" : [5, 1]}}})
--------------------------------------------------------------------------------
Regex db.users.find({"name" : /zorpia/i}) 支援perl的pcre庫來匹配Regex
查詢name 不以T開頭的資料 db.c1.find({name: {$not: /^T.*/}});
--------------------------------------------------------------------------------
查詢數組 db.zorpia.insert({"fruit" : ["apple","banana","peach"]})
用下面的查詢: db.zorpia.find({"fruit" : "banana"})
1.$all 如果需要通過多個元素來匹配數組,用$all,例如: db.zorpia.insert({"fruit" : ["apple","banana","peach"]}) db.zorpia.insert({"fruit" : ["apple","kumquat","orange"]}) db.zorpia.insert({"fruit" : ["cherry","banana","apple"]}) 要找到有apple和banana的文檔,用$all db.zorpia.find({"fruit" : {$all : ["apple", "banana"]}}) 要是想查詢數組指定位置的元素,則用文法指定下標,例如: db.zorpia.find({"fruit.2" :"peach"}) 上面這個會匹配第三個元素是peach的文檔
2.$size數組元素個數 用來查詢指定長度的數組 db.zorpia.find({"fruit" : {"$size" : 3}}) 查詢3個長度的數組,但是他不能和其他查詢子句組合,例如$gt
3.$slice 返回前10條評論 db.zorpia.post.findOne(criteria, { "comments" : {$slice : 10}}) 返回最後10條 db.zorpia.post.findOne(criteria, { "comments" : {$slice : -10}}) 返回24-33個元素 db.zorpia.post.findOne(criteria, { "comments" : {$slice : [23,10]}}) 不過他們都會返回所有的鍵
--------------------------------------------------------------------------------
查詢內嵌文檔 "name" : { "firstname" : "joe", "lastname" : "schmoe" }
可以用下面的查詢joe schmoe的人,很煩,firstname和lastname一定要寫全,而且first和last的次序不能點到。
> db.users.findOne({"name" : {"firstname":"joe","lastname" : "schmoe"}})
正確的方法,用。號 > db.users.findOne({"name.firstname":"joe"}) { "_id" : ObjectId("4e0310f40611960df1e86a95"), "emails" : [ "[email protected]", "[email protected]", "[email protected]" ], "name" : { "firstname" : "joe", "lastname" : "schmoe" } } 注意,點標記法也是待插入的文檔不能包含“。”。如果我們儲存url就會碰到問題。解決方案是插入前或者提取後執行一個全域替換,將。替換為url中的非法字元。
當文檔結構複雜後。假設有部落格文章若干,要找到joe發飆的5分以上的評論。部落格文章的結構如下: > db.blog.find() { "content" : "...", "comments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, { "author" : "mary", "score" : 6, "comment" : "6 fen" } ] } 只能用$elemMatch進行匹配。這種模糊的命名條件能用來部分指定匹配數組中的單個內嵌文檔的限定條件。 db.blog.find({"comments" : { "$elemMatch" : {"author" : "joe","score" : {"$gte" : 5}}}})
--------------------------------------------------------------------------------
$where p55 它可以執行任意JavaScript作為查詢的一部分。這方面我還不是很明白,以後再研究。需要大家自己研究了。
注意:盡量避免使用$where查詢。速度上要比常規查詢慢很多。每個文檔要從bson轉換成JavaScript對象,然後通過$where的運算式來運行。同樣他不能利用索引。
--------------------------------------------------------------------------------
遊標 var cursor = db.users.find() > while (cursor.hasNext()) { obj = cursor.next(); }
cursor.hasNext()檢查時候有後續結果存在,然後用cursor.next()將其獲得。
也能用forEach: var cursor = db.users.find() cursor.forEach(function(x) { print(x.name); });
--------------------------------------------------------------------------------
limit,skip和sort
limit函數,例如只返回3個結果。 db.users.find().limit(3) limit是上限,不是下限
skip與limit類似: db.users.find().skip(3) 會略過前三個匹配的文檔,然後返回餘下的文檔。如果集合少於三個文檔,則不會返回任何文檔。
sort用一個對象作為參數:1代表升序,-1降序。如果指定了多個鍵,則按照多個鍵的順序依次排序。 db.users.find().sort({"username" : 1, "age" : -1})
以上三個方法可以組合使用。處理分頁很有用。例如,有人想搜尋mp3。如果每頁返回50個結果,而且按照價格從高到底排序: db.stock.find({"desc" : "mp3"}).limit(50).sort({"price" : -1})
下一頁只要略過50個就行了。 db.stock.find({"desc" : "mp3"}).skip(50).limit(50).sort({"price" : -1})
應當避免略過過多的結果。
統計 db.things.count(); // select count(*) from things; db.things.find({ x: 10}).count(); // select count(*) from things where x=10; 以下返回的不是5,而是user 表中所有的記錄數量 db.users.find().skip(10).limit(5).count(); 如果要返回限制之後的記錄數量,要使用count(true)或者count(非0) db.users.find().skip(10).limit(5).count(true);
多條件查詢 db.things.find( { x : 3, y : "foo" } ); // where x = 3 and y = "foo"
指定返回結果集條件 db.things.find().limit(10); // limit 10, db.things.find().skip(7).limit(10); // limit 7, 10
排序 db.things.find().sort({$natural: 1}); // order by _id ASE db.things.find().sort({$natural: -1}); // order by _id DESE db.things.find().sort({x: 1}); // order by x ASC db.things.find().sort({x: -1}); //order by x DESC db.things.find().sort({x: 1, y: -1}); // ~.~
模數 db.things.find( { x : { $mod : [ 10 , 1 ] } } ); // where x=1
與數值元素匹配 db.things.find({ x : { $in: [2, 4, 6]}}); // x in array db.things.find({ x: { $nin: [2, 4, 6]}}); // x not in array
$all 匹配所有 這個操作符跟SQL文法的in類似,但不同的是, in只需滿足( )內的某一個值即可, 而$all必
須滿足[ ]內的所有值,例如: db.users.find({age : {$all : [6, 8]}}); 可以查詢出 {name: ‘David‘, age: 26, age: [ 6, 8, 9 ] } 但查詢不出 {name: ‘David‘, age: 26, age: [ 6, 7, 9 ] }
$exists 判斷欄位是否存在
查詢所有存在age欄位的記錄 db.users.find({age: {$exists: true}}); 查詢所有不存在name欄位的記錄 db.users.find({name: {$exists: false}});
Null 值處理
> db.c2.find({age:null})
$mod 模數運算
查詢age模數6等於1的資料
db.c1.find({age: {$mod : [ 6 , 1 ] } })
$ne 不等於
查詢x的值不等於3 的資料 db.c1.find( { age : { $ne : 7 } } );
Javascript 查詢和$Where查詢
查詢a大於3的資料,下面的查詢方法殊途同歸 db.c1.find( { a : { $gt: 3 } } ); db.c1.find( { $where: "this.a > 3" } ); db.c1.find("this.a > 3"); f = function() { return this.a > 3; } db.c1.find(f);
預存程序
關於預存程序你需要知道的第一件事就是它是用 javascript來寫的。 MongoDB 預存程序是儲存在db.system.js表中的,我們想象一個簡單的sql自訂函數如下:
function addNumbers( x , y ) {
return x + y; } 下面我們將這個sql自訂函數轉換為MongoDB 的預存程序:
> db.system.js.save({_id:"addNumbers", value:function(x, y){ return x + y; }});
預存程序可以被查看,修改和刪除,所以我們用 find 來查看一下是否這個預存程序已經被建立上了。 > db.system.js.find() 調用一下這個預存程序: db.eval_r(‘addNumbers(3, 4.2)‘);
db.eval_r()是一個比較奇怪的東西,我們可以將預存程序的邏輯直接在裡面並同時調用,而無需事先聲明預存程序的邏輯。
db.eval_r( function() { return 3+3; }
mongodb查詢find(