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

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

全部操作过程
2020MongoDB基本命令-C操作(增)
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 小于等于
$in in
$nin not 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 }

上一篇:mongodb _id 转时间


下一篇:Mongodb 基础 之 初识