MongoDB CRUD之R

Posted onetoinf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB CRUD之R相关的知识,希望对你有一定的参考价值。

文档查询

命令 操作
db.mycoll.find(key:value) 查询符条件的数据
db.mycoll.find(...).count() 查询并计数
db.mycoll.find(...).limit(n) 限制查询条数
db.mycoll.find(...).skip(n) 跳过查询
db.mycoll.find(...).sort(...) 查询并排序
db.mycoll.find(...).pretty() 查询并优化输出结果

Read

查看所有

25 > db.set2test.find()
{ "_id" : ObjectId("5a7943273fdd5fec85460638"), "x" : 3 }
{ "_id" : ObjectId("5a7943273fdd5fec85460639"), "x" : 4 }
{ "_id" : ObjectId("5a7943273fdd5fec8546063a"), "x" : 5 }
.......
{ "_id" : ObjectId("5a7943273fdd5fec8546064b"), "x" : 22 }
Type "it" for more
26 > it
{ "_id" : ObjectId("5a7943273fdd5fec8546064c"), "x" : 23 }
{ "_id" : ObjectId("5a7943273fdd5fec8546064d"), "x" : 24 }
{ "_id" : ObjectId("5a7943273fdd5fec8546064e"), "x" : 25 }

输入it查看更多数据

指定字段

44 > db.unicorns.find({},{_id: 0, name: 1, gender: 1, weight: 1})
{ "name" : "Aurora", "gender" : "f", "weight" : 450 }
{ "name" : "Horny", "weight" : 600, "gender" : "m" }
{ "name" : "Aurora", "weight" : 450, "gender" : "f" }
{ "name" : "Unicrom", "weight" : 984, "gender" : "m" }
{ "name" : "Roooooodles", "weight" : 575, "gender" : "m" }
{ "name" : "Solnara", "weight" : 550, "gender" : "f" }
{ "name" : "Ayna", "weight" : 733, "gender" : "f" }
{ "name" : "Kenny", "weight" : 690, "gender" : "m" }
{ "name" : "Raleigh", "weight" : 421, "gender" : "m" }
{ "name" : "Leia", "weight" : 601, "gender" : "f" }
{ "name" : "Pilot", "weight" : 650, "gender" : "m" }
{ "name" : "Nimue", "weight" : 540, "gender" : "f" }
{ "name" : "Dunx", "weight" : 704, "gender" : "m" }

条件查询

45 > db.unicorns.find({weight: {$gt: 700}},{_id: 0, name: 1, gender: 1, weight: 1})
{ "name" : "Unicrom", "weight" : 984, "gender" : "m" }
{ "name" : "Ayna", "weight" : 733, "gender" : "f" }
{ "name" : "Dunx", "weight" : 704, "gender" : "m" }
大小
操作符 意义 符号
$gt greater than >
$gte gt equal >=
$lt less than <
$lte lt equal <=
$ne not equal !=
$eq equal =
41 > db.unicorns.find({weight: {$gt: 700}}).count()
3
逻辑
操作符 意义 符号
$or or ||
$and and &&
48 > db.unicorns.find(
{
    $and: 
    [{weight: {$gt: 700}},{gender: 'f'}]
    }, 
{
    _id: 0, 
    name: 1, 
    gender: 1, weight: 1
    }
)
{ "name" : "Ayna", "weight" : 733, "gender" : "f" }
49 > 

等价于

49 > db.unicorns.find(
    {
        weight: {$gt: 700},gender: 'f'
        }, 
    {
        _id: 0, name: 1, gender: 1, weight: 1
        }
)
{ "name" : "Ayna", "weight" : 733, "gender" : "f" }
模糊
  • 正则
54 > db.unicorns.find({name: {$regex: /^[A-D]/}}, {_id: 0, name: 1})
{ "name" : "Aurora" }
{ "name" : "Aurora" }
{ "name" : "Ayna" }
{ "name" : "Dunx" }
  • 键值存在与否
61 > db.unicorns.find({ loves: { $exists: false } }, {_id: 0})
{ "name" : "Aurora", "gender" : "f", "weight" : 450 }
  • 范围
64 > db.unicorns.find({ name: { $in: ["Ayna", "Aurora"] } }, {_id: 0, name: 1, gender: 1, weight: 1})
{ "name" : "Aurora", "gender" : "f", "weight" : 450 }
{ "name" : "Aurora", "weight" : 450, "gender" : "f" }
{ "name" : "Ayna", "weight" : 733, "gender" : "f" }

类似的还有$nin

更多的查询操作符看这里

输出优化

28 > db.unicorns.find({gender:'f'}).pretty()
{
    "_id" : ObjectId("5a793d993fdd5fec8546062b"),
    "name" : "Aurora",
    "gender" : "f",
    "weight" : 450
}
{
    "_id" : ObjectId("5a79408c3fdd5fec8546062d"),
    "name" : "Aurora",
    "dob" : ISODate("1991-01-24T05:00:00Z"),
    "loves" : [
        "carrot",
        "grape"
    ],
    "weight" : 450,
    "gender" : "f",
    "vampires" : 43
}
......

排序

升序
30 > db.unicorns.find().sort({weight:1})
{ ... "weight" : 421,... }
{ ... "weight" : 450 }
{ ... "weight" : 450, "gender" : "f", "vampires" : 43 }
{ ... "weight" : 540, "gender" : "f" }
{ ... "weight" : 550, "gender" : "f", "vampires" : 80 }
{ ... "weight" : 575, "gender" : "m", "vampires" : 99 }
{ ... "weight" : 600, "gender" : "m", "vampires" : 63 }
{ ... "weight" : 601, "gender" : "f", "vampires" : 33 }
{ ... "weight" : 650, "gender" : "m", "vampires" : 54 }
{ ... "weight" : 690, "gender" : "m", "vampires" : 39 }
{ ... "weight" : 704, "gender" : "m", "vampires" : 165 }
{ ... "weight" : 733, "gender" : "f", "vampires" : 40 }
{ ... "weight" : 984, "gender" : "m", "vampires" : 182 }
降序
31 > db.unicorns.find().sort({weight:-1})
{ ... "weight" : 984, "gender" : "m", "vampires" : 182 }
{ ... "weight" : 733, "gender" : "f", "vampires" : 40 }
{ ... "weight" : 704, "gender" : "m", "vampires" : 165 }
{ ... "weight" : 690, "gender" : "m", "vampires" : 39 }
{ ... "weight" : 650, "gender" : "m", "vampires" : 54 }
{ ... "weight" : 601, "gender" : "f", "vampires" : 33 }
{ ... "weight" : 600, "gender" : "m", "vampires" : 63 }
{ ... "weight" : 575, "gender" : "m", "vampires" : 99 }
{ ... "weight" : 550, "gender" : "f", "vampires" : 80 }
{ ... "weight" : 540, "gender" : "f" }
{ ... "weight" : 450 }
{ ... "weight" : 450, "gender" : "f", "vampires" : 43 }
{ ... "weight" : 421, "gender" : "m", "vampires" : 2 }
分页查询
37 > db.unicorns.find().sort({weight:-1}).limit(2).skip(10).pretty()
{
    "_id" : ObjectId("5a793d993fdd5fec8546062b"),
    "name" : "Aurora",
    "gender" : "f",
    "weight" : 450
}
{
    "_id" : ObjectId("5a79408c3fdd5fec8546062d"),
    "name" : "Aurora",
    "dob" : ISODate("1991-01-24T05:00:00Z"),
    "loves" : [
        "carrot",
        "grape"
    ],
    "weight" : 450,
    "gender" : "f",
    "vampires" : 43
}

计数

39 > db.unicorns.find({gender: 'f'}).count()
6

去重

67 > db.unicorns.distinct("gender")
[ "f", "m" ]

以上是关于MongoDB CRUD之R的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB 学习笔记之 基本CRUD

MongoDB CRUD之U

MongoDB CRUD之D

《MongoDB入门教程》第06篇 CRUD之创建文档

《MongoDB入门教程》第06篇 CRUD之创建文档

《MongoDB入门教程》第06篇 CRUD之创建文档