1. 程式人生 > 資料庫 >2020MongoDB基本命令-R操作(查)

2020MongoDB基本命令-R操作(查)

2020MongoDB基本命令-R操作(查)

全部操作過程

  1. 基本語句

    db.集合名.find([條件],[查詢的列])
    

    條件
    查詢所有資料 {}或者不寫
    查詢age=10,性別為男 {age:10,sex=‘男’}
    查詢的列(可選)
    {age:1}只顯示age列
    {age:0}除了age其餘的都顯示
    無論寫什麼,_id都會存在

    示例1.(查詢資料庫中所有的資料)

    //使用test2資料庫
    use test2
    // 查詢所有集合
    show collections
    db.c1.find()
    

    結果1.

    { “_id” : ObjectId(“5fc22f77451fb8b5be3c3f6e”), “uname” : “ppl”, “age” : 18 }

    { “_id” : 1, “uname” : “ppl”, “age” : 18 }
    { “_id” : ObjectId(“5fc23651451fb8b5be3c3f6f”), “username” : “a1”, “age” : 99 }
    { “_id” : ObjectId(“5fc23651451fb8b5be3c3f70”), “username” : “a2”, “age” : 89 }
    { “_id” : ObjectId(“5fc23651451fb8b5be3c3f71”), “username” : “a3”, “age” : 79 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f72”), “uname” : “b0”, “age” : 50 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f73”), “uname” : “b1”, “age” : 51 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f74”), “uname” : “b2”, “age” : 52 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f75”), “uname” : “b3”, “age” : 53 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f76”), “uname” : “b4”, “age” : 54 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f77”), “uname” : “b5”, “age” : 55 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f78”), “uname” : “b6”, “age” : 56 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f79”), “uname” : “b7”, “age” : 57 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f7a”), “uname” : “b8”, “age” : 58 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f7b”), “uname” : “b9”, “age” : 59 }

    示例2.(查詢uname列)

    db.c1.find({},{uname:1})
    

    結果2.

    { “_id” : ObjectId(“5fc22f77451fb8b5be3c3f6e”), “uname” : “ppl” }
    { “_id” : 1, “uname” : “ppl” }
    { “_id” : ObjectId(“5fc23651451fb8b5be3c3f6f”) }
    { “_id” : ObjectId(“5fc23651451fb8b5be3c3f70”) }
    { “_id” : ObjectId(“5fc23651451fb8b5be3c3f71”) }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f72”), “uname” : “b0” }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f73”), “uname” : “b1” }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f74”), “uname” : “b2” }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f75”), “uname” : “b3” }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f76”), “uname” : “b4” }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f77”), “uname” : “b5” }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f78”), “uname” : “b6” }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f79”), “uname” : “b7” }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f7a”), “uname” : “b8” }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f7b”), “uname” : “b9” }

    示例3.(查詢除了uname以外的其餘的列)

    db.c1.find({},{uname:0})
    

    結果3.

    { “_id” : ObjectId(“5fc22f77451fb8b5be3c3f6e”), “age” : 18 }
    { “_id” : 1, “age” : 18 }
    { “_id” : ObjectId(“5fc23651451fb8b5be3c3f6f”), “username” : “a1”, “age” : 99 }
    { “_id” : ObjectId(“5fc23651451fb8b5be3c3f70”), “username” : “a2”, “age” : 89 }
    { “_id” : ObjectId(“5fc23651451fb8b5be3c3f71”), “username” : “a3”, “age” : 79 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f72”), “age” : 50 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f73”), “age” : 51 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f74”), “age” : 52 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f75”), “age” : 53 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f76”), “age” : 54 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f77”), “age” : 55 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f78”), “age” : 56 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f79”), “age” : 57 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f7a”), “age” : 58 }
    { “_id” : ObjectId(“5fc238ac451fb8b5be3c3f7b”), “age” : 59 }

2.運算子

運算子功能
$gt大於
$gte大於等於
$lt小於
$lte小於等於
$inin
$ninnot in

示例1. 查詢年齡大於55歲的使用者

db.c1.find({age:{$gt:55}})

結果1.

{ “_id” : ObjectId(“5fc23651451fb8b5be3c3f6f”), “username” : “a1”, “age” : 99 }
{ “_id” : ObjectId(“5fc23651451fb8b5be3c3f70”), “username” : “a2”, “age” : 89 }
{ “_id” : ObjectId(“5fc23651451fb8b5be3c3f71”), “username” : “a3”, “age” : 79 }
{ “_id” : ObjectId(“5fc238ac451fb8b5be3c3f78”), “uname” : “b6”, “age” : 56 }
{ “_id” : ObjectId(“5fc238ac451fb8b5be3c3f79”), “uname” : “b7”, “age” : 57 }
{ “_id” : ObjectId(“5fc238ac451fb8b5be3c3f7a”), “uname” : “b8”, “age” : 58 }
{ “_id” : ObjectId(“5fc238ac451fb8b5be3c3f7b”), “uname” : “b9”, “age” : 59 }

示例2.查詢使用者年齡在55,57,79歲的資訊

db.c1.find({age:{$in:[55,57,59]}})

結果2.

{ “_id” : ObjectId(“5fc238ac451fb8b5be3c3f77”), “uname” : “b5”, “age” : 55 }
{ “_id” : ObjectId(“5fc238ac451fb8b5be3c3f79”), “uname” : “b7”, “age” : 57 }
{ “_id” : ObjectId(“5fc238ac451fb8b5be3c3f7b”), “uname” : “b9”, “age” : 59 }