集合簡單查詢方法
mongodb語法:db.collection.find() //collection就是集合的名稱,這個可以自己進行建立。
對比sql語句:select * from collection;
查詢集合中所有的文件,即關係型資料庫中的查詢表中的所有資料。
返回制定的鍵值
mongodb語法:db.collection.find({},)
對比sql語句:select userid from collection;
條件過濾
mongodb語法 : db.collection.find()
對比sql語句:select * from collectionwhere userid = 495;
查詢全格式書寫解釋
db.collection.find({},{})
第乙個{}中,寫入的都是相當於sql語句中where後的條件,多條件格式為
第二個{}中,寫入的都是相當於sql語句中跟在select後邊的具體字段,格式為
當value = 0時為不顯示此字段值,當value !=0,即等於任何非0值時,則為顯示此欄位。
例:mongodb查詢:
db.error.find(,)
sql查詢:
select userid,type,message from error where userid=1 and type = "debug";
sort排序與limit返回固定條目數
mongodb查詢:
db.collection.find(,).sort().limit(10)
sql查詢:
select userid,type,message from collection where userid=1 and type = "debug" order by time desc limit 10;
count返回結果集總數
mongodb查詢:
db.collection.count()
sql查詢:
select count(*) from collection;
查詢操作符"$gt" -->大於操作符
mongodb查詢:
db.collection.find(})
sql查詢:
select * from collection where userid > 494;
查詢操作符"$gte" -->大於等於
mongodb查詢:
db.collection.find(})
sql查詢:
select * from collection where userid >= 494;
查詢操作符 "$lt"-->小於
mongodb查詢:
db.collection.find(})
sql查詢:
select * from collection where userid <494;
查詢操作符"$lte"-->小於等於
mongodb查詢:
db.collection.find(})
sql查詢:
select * from collection where userid < =494;
查詢操作符"$ne"-->不等於
mongodb查詢:
db.collection.find(})
sql查詢:
select * from collection where userid != 494;
查詢操作符"null查詢"-->空
mongodb查詢:
db.collection.find()
sql查詢:
select * from collection where userid is null;
查詢操作符"$all"-->匹配所有
mongodb查詢:
db.collection.find(})
sql查詢:
select * from collection where userid = 494;
當文件型別為陣列時,使用$all進行匹配,非陣列型別使用時與單一匹配一樣。
查詢操作符"$size"-->用於陣列查詢,查詢指定長度的陣列
mongodb查詢:
db.collection.find(})
查詢操作符"$in"--> 在範圍內
mongodb查詢:
db.collection.find(})
sql查詢:
select * from collection where userid in (297,495);
查詢操作符"$nin"-->不在範圍內
mongodb查詢:
db.collection.find(})
sql查詢:
select * from collection where userid not in (297,495);
查詢操作符"$and"-->至少包含兩個表示式,兩個表示式都滿足的文件返回
mongodb查詢:
db.collection.find(,]})
sql查詢:
select * from collection where userid=495 and type='info';
查詢操作符"$nor"-->至少包含兩個表示式,兩個表示式都不滿足的文件返回
mongodb查詢:
db.collection.find(,]})
sql查詢:
select * from collection where userid not in (297,495);
查詢操作符"$not"-->找出不匹配表示式的文件,不能夠單獨使用,必須與其他表示式配合使用
mongodb查詢:
db.collection.find(}})
等同於:db.collection.find(}})
sql查詢:
select * from collection where userid <=297;
查詢操作符"$or"-->至少包含兩個表示式,兩個表示式至少滿足乙個的文件返回
mongodb查詢:
db.collection.find(,]})
sql查詢:
select * from collection where userid =297 or userid = 495;
查詢操作符"$exists"-->查詢文件中欄位是否存在
mongodb查詢:
db.collection.find()
查詢操作符"$mod"-->鍵值對變數引數取模,值等於另乙個引數
mongodb查詢:
db.collection.find(})
執行條件:userid字段值必須是數字,userid對10取模,值等於7的文件返回。
sql查詢:
select * from collection where (user_id%10)=7
查詢操作符"$regex"-->正則匹配
mongodb查詢:
db.collection.find()
sql查詢:
select * from collection where userid like '%5';
sql正則寫法:
select * from collection where userid regexp ".5$";
正則匹配userid的最後一位是5的,sql中只有使用regexp才可以使用複雜的正規表示式,使用like的方式不可以進行複雜的正則匹配
查詢操作符"$slice"-->控制返回的陣列元素中的元素個數
mongodb查詢:
db.collection.find({},)
remark陣列鍵值,返回陣列鍵值中的前5個元素
db.collection.find({},)
remark陣列鍵值,返回陣列鍵值中的第11到15個元素,偏移量為10,然後返回5個。
db.collection.find({},)
remark陣列鍵值,返回陣列鍵值中的後5個元素
mongodb日期範圍 MongoDb查詢日期範圍
mongodb查詢轉物件是出錯element id does not match any field or property of class mongodb查詢轉物件是出錯element id does not match any field or property of class 解決方法 1...
MongoDB分頁查詢的方法及效能
自從上次redis之後呢,算是對nosql型別的產品有些入門了,這會換個方向,研究下真正的nosql資料庫 mongodb。說起mongodb,確實是用完了之後顛覆了我的資料管和程式觀。怎麼說呢?如果用在oo設計的程式裡那真的太棒了,像我這種資料驅動 表驅動思想根深蒂固的人,思路很難一下子跟上mon...
Mongodb查詢文件find方法的使用
連線mongodb 建立集合規則 使用集合規則建立集合 使用find方法查詢文件 1.連線mongodb 引用第三方模組 mongoose const mongoose require mongoose 鏈結伺服器 返回promise 物件 mongoose.connect mongodb loca...