时间:2021-07-01 10:21:17 帮助过:34人阅读
命令 | 操作 |
---|---|
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() |
查询并优化输出结果 |
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
标签:模糊 ali tty operator pre iso ror als 数据