mongoDB 文檔操做_查

基本查詢命令

find

查找複合條件的全部文檔html

命令

db.collection.find(query,field)

參數

query 查找條件
    格式: {ssss:"xxx"}是一個鍵值對構成的文檔
    若是是空, 表示查找全部內容
field 查找的域
    格式: {ssss:"xxx"}是一個鍵值對構成的文檔
    設置值:
        設置爲1 表示查找該域,其餘自動爲 0
        設置爲0 表示不查找該域,其餘自動爲 1
        設置的時候要不所有爲 1 或者所有 爲 0 是不能混搭的
        _id 域若是沒寫使用被選中,不會受其餘影響
        _id:0 才能夠不顯示
        若是是空, 表示查找全部域

返回值

返回查找到的全部文檔mongodb

注意點

query 參數不傳遞, 是沒法傳遞 field 參數的數組

若是無 query 參數,且使用 field 參數則須要 query 設置成 {} 便可ide

實例

查詢全部含有  age:18 鍵值對的文檔
輸入: > db.class0.find({age:18})
輸出:
    { "_id" : ObjectId("5c76550eb9330b7c15210101"), "name" : "tuo", "age" : 18, "sex" : "man" }
    { "_id" : ObjectId("5c765554b9330b7c15210102"), "name" : "jlsajdla", "age" : 18, "sex" : "man" }
查找全部文檔,僅顯示 name,age 字段
輸入: > db.class0.find({},{_id:0,name:1,age:1})
輸出:
    { "name" : "yang", "age" : 16 }
    { "name" : "tuo", "age" : 18 }
    { "age" : 18 }
    { "age" : 18 }

findOne

查找第一條符合條件的文檔函數

命令

db.collection.findOne(query,field)

參數

用法同 find學習

返回值

同 findspa

> db.class0.find()
{ "_id" : ObjectId("5c765408b9330b7c15210100"), "name" : "yang", "age" : 16 }
{ "_id" : ObjectId("5c76550eb9330b7c15210101"), "name" : "tuo", "age" : 18, "sex" : "man" }
{ "_id" : ObjectId("5c765554b9330b7c15210102"), "Null" : "tuo", "age" : 18, "sex" : "man" }
{ "_id" : ObjectId("5c765568b9330b7c15210103"), "null" : "tuo", "age" : 18, "sex" : "man" }
{ "_id" : 1, "name" : "tuo", "age" : 18, "sex" : "man" }
{ "_id" : 2, "name" : "tuo", "age" : 18, "sex" : "man" }
{ "_id" : 3, "name" : "4tuo", "age" : 418, "sex" : "man" }
{ "_id" : 4, "name" : "hjsdj", "age" : 18 }
{ "_id" : ObjectId("5c765918b9330b7c15210104"), "name" : "4tuo", "age" : 418, "sex" : "man" }
{ "_id" : ObjectId("5c765918b9330b7c15210105"), "name" : "hjsdj", "age" : 18 }
{ "_id" : ObjectId("5c76592db9330b7c15210106"), "name" : "jj", "age" : 18 }
{ "_id" : ObjectId("5c765a8ab9330b7c15210107"), "name" : "ub", "age" : 88, "sex" : "man" }
> db.class0.find({name:"tuo"},{_id:0,sex:1})
{ "sex" : "man" }
{ "sex" : "man" }
{ "sex" : "man" }
> db.class0.find({name:"tuo"},{_id:0,sex:1,age:1})
{ "age" : 18, "sex" : "man" }
{ "age" : 18, "sex" : "man" }
{ "age" : 18, "sex" : "man" }
> db.class0.find({name:"tuo"},{_id:0,sex:1,age:0})
Error: error: {
    "ok" : 0,
    "errmsg" : "Projection cannot have a mix of inclusion and exclusion.",
    "code" : 2,
    "codeName" : "BadValue"
}
>
> db.class0.find({},{_id:0,sex:1,age:1})
{ "age" : 16 }
{ "age" : 18, "sex" : "man" }
{ "age" : 18, "sex" : "man" }
{ "age" : 18, "sex" : "man" }
{ "age" : 18, "sex" : "man" }
{ "age" : 18, "sex" : "man" }
{ "age" : 418, "sex" : "man" }
{ "age" : 18 }
{ "age" : 418, "sex" : "man" }
{ "age" : 18 }
{ "age" : 18 }
{ "age" : 88, "sex" : "man" }
>
> db.class0.findOne({},{_id:0,sex:1,age:1})
{ "age" : 16 }
>
find findOne 實例合集

query 操做符

定義

MongoDB 中使用 $ 符號註明的有特殊意義的字符串,用於表達豐富的含義操作系統

格式

{name{$eq:17}} 形同 {name:17}code

使用位置

query 位置htm

比較運算操做符

$eq     等於
$lt     小於
$lte    小於等於
$gt     大於
$gte    大於等於
$ne     不等於

使用格式:

query : {field:{$操做符:值}}

ps:

  {age:{$gt:17,$lt:20}} 能夠多個條件並列, 以 and 的關係.

操做實例

age < 18 
> db.class0.find({age:{$lt:18}})
age
> 18 > db.class0.find({age:{$gt:18}})
age
= 18 > db.class0.find({age:{$eq:18}})
name
> y > db.class0.find({name:{$gt:"y"}})
15 < age < 20 > db.class0.find({age:{$gt:15,$lt:20}})
age
>= 18 > db.class0.find({age:{$gte:18}})
age
<= 18 > db.class0.find({age:{$lte:18}})
age
!= / <> 18 > db.class0.find({age:{$ne:18}})

範圍操做符

$in 

存在範圍內

格式 :  

query: {field:{$in:[xx,xx,xx,xx]}}

> db.class0.find({age:{$in:[1,16,18]}})

$nin 

不存在範圍內

格式 :  

query: {field:{$nin:[xx,xx,xx,xx]}}

> db.class0.find({age:{$nin:[1,16,18]}})

 

 邏輯操做符

$and  與/且 

{$and[{},{}]}  徹底等同於  {{},{}}

默認的 " ,  " 相連就是 $and 的關係

獲取 年級大於18 且 性別爲男的文檔
> db.class0.find({$and:[{age:{$gt:18}},{sex:"man"}]},{_id:0})
獲取 年級大於18 且 性別爲男的文檔
> db.class0.find({age:{$gt:18}},{sex:"man"},{_id:0})

$or 或

{$or[{},{}]} 

 獲取 年紀小於18 或者 性別爲男的文檔
 > db.class0.find({$or:[{age:{$lt:18}},{sex:"man"}]},{_id:0})

$not 非

{$not:{...}}    單目運算

獲取 性別 不是 男性 的文檔
> db.class0.find({sex:{$not:{$eq:"man"}}})

 這裏必需要用 $eq 來做爲鏈接賦值判斷, $not 後面不能直接跟值來判斷. 

> db.class.find({sex:{$not:"b"}},{_id:0})
Error: error: {
    "ok" : 0,
    "errmsg" : "$not needs a regex or a document",
    "code" : 2,
    "codeName" : "BadValue"
}

$nor 亦或(既不 也不)

not (A or B)

A,B 全都是假才能夠是真

{$nor:[{},{}]}  

獲取既不是 男性 又不大於18歲 的文檔
> db.class0.find({$nor:[{age:{$lt:18}},{sex:"man"}]},{_id:0})

混合語句

年齡大於18 或者小於17 而且 性別爲男
> db.class0.find({$or:[{age:{$gt:18}},{age:{$lt:17}}],sex:"man"},{_id:0})
年齡大於17 的男生 或者 姓名叫作 yang 或者 tuo
> db.class0.find({$or:[{name:"yang"},{name:"tuo"}],sex:"man",age:{$gt:17}},{_id:0})

數組操做符

數組: 一組數據的有序集合,用[]表示

普通查詢

查詢的時候會把只要知足任意條件的都拿出來

 含有 18 就算
 > db.class.find({score:18},{_id:0})
 { "name" : "yy", "age" : 8, "score" : [ 56, 18, 75 ] }
有一個超過 60 就算
> db.class.find({score:{$gt:60}},{_id:0})
{ "name" : "yang", "age" : 6, "score" : [ 98, 56, 32 ] }
{ "name" : "tuo", "age" : 9, "score" : [ 82, 56, 81 ] }
{ "name" : "yy", "age" : 8, "score" : [ 56, 18, 75 ] }

$all  知足全部條件

必須包含 82 和 81 
> db.class.find({score:{$all:[82,81]}},{_id:0})
{ "name" : "tuo", "age" : 9, "score" : [ 82, 56, 81 ] }

$size  數組長度

取出數組長度爲 2 的文檔
> db.class.find({score:{$size:2}},{_id:0})
{ "name" : "zz", "age" : 8, "score" : [ 55, 78 ] }

$slice  切片

用於 filed 參數

單數值表示顯示數組的前幾項

列表傳入兩個值 表示顯示數組的起點,以及起點後幾項

既然是切片那天然就 左不包含,右包含

取出數組前兩項
> db.class.find({},{_id:0,score:{$slice:2}})
從數組第一項日後取兩項,不包括第一項
> db.class.find({},{_id:0,score:{$slice:[1,2]}})
從數組的第三項日後取兩項,不包括第三項
> db.class.find({},{_id:0,score:{$slice:[3,2]}}) { "name" : "yang", "age" : 6, "score" : [ ] }

其餘操做符

$exists  判斷域是否存在

取出不含有 score 域的文檔
> db.class.find({score:{$exists:false}},{_id:0})
{ "name" : "pp", "age" : 18 }

取出含有 score 域的文檔
> db.class.find({score:{$exists:true}},{_id:0})

$mod  除數,餘數搜索

找 age 爲偶數 的文檔
> db.class.find({age:{$mod:[2,0]}},{_id:0})
找age 爲奇數 的文檔
> db.class.find({age:{$mod:[2,1]}},{_id:0})

$type  根據類型查找

{$type:int}  數字爲類型標號, 具體類型編號參考 這裏

查找的數值爲 類型的標識數字
> db.class.find({name:{$type:2}},{_id:1})

注意點:

雖然 score 裏面是個數組,可是find 查詢的時候取出來的不是數組總體而是裏面的值

所以除非 score:[[1,2,3],4,5] 這樣值裏面還有數組才能夠被命中.

僅僅是 score:[1,2,3,4,5] 這樣取出來的其實只是 1,2,3,4,5 這樣的數字

 

按理說查找 16 的 32bit-int 數字, 可是依舊查不到

其實本質上由於操做系統的轉換自動幫你轉換成了 浮點型,所以使用 1 才能夠查到

> db.class.find({score:{$type:4}},{_id:1})
>
> db.class.find({score:{$type:16}},{_id:1})
>
> db.class.find({score:{$type:1}},{_id:1})
{ "_id" : ObjectId("5c775857c69c81d07212f58a") }
{ "_id" : ObjectId("5c775887c69c81d07212f58b") }
{ "_id" : ObjectId("5c7758e8c69c81d07212f58c") }
{ "_id" : ObjectId("5c775b82bed69fac33334adf") }
>

額外

其餘操做符不會的能夠在官網查看說明進行學習使用 官方文檔

數據操做函數

在被查詢出來的文檔集合後再一次的過濾查詢獲得更精確的結果

distinct(field)

db.collection.distinct(field)

功能:

獲取某個集合中某個域的值範

參數:

域名

返回值:

獲取範圍數組(去重的)

實例

查看 age 域 有哪些值
> db.class.distinct("age")
[ 6, 9, 8, 18 ]

pretty()

功能

將find查找結果格式化顯示,每一個域單行顯示,沒什麼實際意義....

無參數

> db.class.find().pretty()
   {
       "_id" : ObjectId("5c775857c69c81d07212f58a"),
       "name" : "yang",
       "age" : 6,
       "score" : [
           98,
           56,
           32
       ]
   }
   ...

limit(n)

功能

顯示查詢結果的前幾項

參數

n 指定顯示數量

查看全部數據的前三條
> db.class.find({},{_id:0}).limit(3)
{ "name" : "yang", "age" : 6, "score" : [ 98, 56, 32 ] }
{ "name" : "tuo", "age" : 9, "score" : [ 82, 56, 81 ] }
{ "name" : "yy", "age" : 8, "score" : [ 56, 18, 75 ] }
>

skip(n)

功能

跳過查詢結果的前幾個,顯示後面的

參數

n 跳過個數

> db.class.find({},{_id:0})
{ "name" : "yang", "age" : 6, "score" : [ 98, 56, 32 ] }
{ "name" : "tuo", "age" : 9, "score" : [ 82, 56, 81 ] }
{ "name" : "yy", "age" : 8, "score" : [ 56, 18, 75 ] }
{ "name" : "zz", "age" : 8, "score" : [ 55, 78 ] }
{ "name" : "pp", "age" : 18 }

查詢全部文檔, 跳過前三個
> db.class.find({},{_id:0}).skip(3) { "name" : "zz", "age" : 8, "score" : [ 55, 78 ] } { "name" : "pp", "age" : 18 }

count()

功能

統計查詢結果

> db.class.find({},{_id:0}).count()
5

ps:

  給出查找條件的時候就是準的,有時候不給條件的時候就有時候會不精準

sort({field:1/-1})

功能

排序

參數

field 排序的域 ,

可選值:

  1 升序

  2 降序

> db.class.find({},{_id:0}).sort({age:1})
{ "name" : "yang", "age" : 6, "score" : [ 98, 56, 32 ] }
{ "name" : "yy", "age" : 8, "score" : [ 56, 18, 75 ] }
{ "name" : "zz", "age" : 8, "score" : [ 55, 78 ] }
{ "name" : "tuo", "age" : 9, "score" : [ 82, 56, 81 ] }
{ "name" : "pp", "age" : 18 }
> db.class.find({},{_id:0}).sort({age:-1})
{ "name" : "pp", "age" : 18 }
{ "name" : "tuo", "age" : 9, "score" : [ 82, 56, 81 ] }
{ "name" : "yy", "age" : 8, "score" : [ 56, 18, 75 ] }
{ "name" : "zz", "age" : 8, "score" : [ 55, 78 ] }
{ "name" : "yang", "age" : 6, "score" : [ 98, 56, 32 ] }
>

也能夠複合排序, 先按照 age 排序, 若是相同, 按照 name 排序 > db.class.find({},{_id:0}).sort({age:-1},{name:-1})

經過連續調用函數獲取精確結果

只要你返回的是文檔集合(count 就不行), 理論上你就能夠無限調用下去

> db.class.find({},{_id:0}).limit(5).skip(3).sort({age:1}).pretty()
{ "name" : "tuo", "age" : 9, "score" : [ 82, 56, 81 ] }
{ "name" : "pp", "age" : 18 }

經過序列號直接查找序列號的某一項

索引序號的方式直接獲取某一項文檔

> db.class.find({},{_id:0}).limit(5).skip(3).sort({age:1}).pretty()
{ "name" : "tuo", "age" : 9, "score" : [ 82, 56, 81 ] }
{ "name" : "pp", "age" : 18 }
> db.class.find({},{_id:0}).limit(5).skip(3).sort({age:1}).pretty()[1]
{ "name" : "pp", "age" : 18 }
>
相關文章
相關標籤/搜索