MongoDB入门到进阶笔记:04-MongoDB 多种查询方式 find pretty limit skip sort
collection数据准备
use wygzs
db.myuser.insert( {name:”shijiange1″, age: 20} )
db.myuser.insert( {name:”shijiange2″, age: 28} )
db.myuser.insert( {name:”shijiange3″, age: 38} )
db.myuser.insert( {name:”zhangsan1″, age: 58} )
db.myuser.insert( {name:”zhangsan2″, age: 68} )
db.myuser.insert( {name:”zhangsan3″, age: 25} )
db.myuser.insert( {name:”shijiange1″, age: 20} )
ert( {name:”zhangsan3″, age: 25} )WriteResult({ “nInserted” : 1 })
db.myuser.insert( {name:”shijiange2″, age: 28} )
WriteResult({ “nInserted” : 1 })
db.myuser.insert( {name:”shijiange3″, age: 38} )
WriteResult({ “nInserted” : 1 })
db.myuser.insert( {name:”zhangsan1″, age: 58} )
WriteResult({ “nInserted” : 1 })
db.myuser.insert( {name:”zhangsan2″, age: 68} )
WriteResult({ “nInserted” : 1 })
db.myuser.insert( {name:”zhangsan3″, age: 25} )
WriteResult({ “nInserted” : 1 })
pretty易读的方式
db.myuser.find().pretty()
db.myuser.find().pretty()
{
“_id” : ObjectId(“6362360295502fbd56836b96”),
“name” : “shijiange1”,
“age” : 20
}
{
“_id” : ObjectId(“6362360295502fbd56836b97”),
“name” : “shijiange2”,
“age” : 28
}
{
“_id” : ObjectId(“6362360295502fbd56836b98”),
“name” : “shijiange3”,
“age” : 38
}
{
“_id” : ObjectId(“6362360295502fbd56836b99”),
“name” : “zhangsan1”,
“age” : 58
}
{
“_id” : ObjectId(“6362360295502fbd56836b9a”),
“name” : “zhangsan2”,
“age” : 68
}
{
“_id” : ObjectId(“6362360295502fbd56836b9b”),
“name” : “zhangsan3”,
“age” : 25
}
limit限制条数查询
db.myuser.find()
db.myuser.find().limit(2) #查看前面两条记录
db.myuser.find().limit(2)
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }
db.myuser.find().limit(2).pretty()
{
“_id” : ObjectId(“6362360295502fbd56836b96”),
“name” : “shijiange1”,
“age” : 20
}
{
“_id” : ObjectId(“6362360295502fbd56836b97”),
“name” : “shijiange2”,
“age” : 28
}
使用skip跳过记录
db.myuser.find().skip(2).limit(2)
db.myuser.find().skip(2)
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
{ “_id” : ObjectId(“6362360295502fbd56836b99”), “name” : “zhangsan1”, “age” : 58 }
{ “_id” : ObjectId(“6362360295502fbd56836b9a”), “name” : “zhangsan2”, “age” : 68 }
{ “_id” : ObjectId(“6362360295502fbd56836b9b”), “name” : “zhangsan3”, “age” : 25 }
db.myuser.find().skip(2).limit(2)
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
{ “_id” : ObjectId(“6362360295502fbd56836b99”), “name” : “zhangsan1”, “age” : 58 }
mongodb分页查询
db.myuser.find().skip(0).limit(2)
db.myuser.find().skip(2).limit(2)
db.myuser.find().skip(4).limit(2)
使用sort进行排序
db.myuser.find().sort({ age: 1 })#按age升序
db.myuser.find().sort({ age: -1 })#按age降序
db.myuser.find().sort({age:1})
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
{ “_id” : ObjectId(“6362360295502fbd56836b9b”), “name” : “zhangsan3”, “age” : 25 }
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
{ “_id” : ObjectId(“6362360295502fbd56836b99”), “name” : “zhangsan1”, “age” : 58 }
{ “_id” : ObjectId(“6362360295502fbd56836b9a”), “name” : “zhangsan2”, “age” : 68 }
db.myuser.find().sort({age:-1})
{ “_id” : ObjectId(“6362360295502fbd56836b9a”), “name” : “zhangsan2”, “age” : 68 }
{ “_id” : ObjectId(“6362360295502fbd56836b99”), “name” : “zhangsan1”, “age” : 58 }
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }
{ “_id” : ObjectId(“6362360295502fbd56836b9b”), “name” : “zhangsan3”, “age” : 25 }
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
根据字段进行数字比较查询
db.myuser.find({ age: {$lt: 30} })
$gt#大于
$lt#小于
$gte#大于或等于
$lte#小于或等于
$eq #等于
db.myuser.find({age:{$lt:30}})
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }
{ “_id” : ObjectId(“6362360295502fbd56836b9b”), “name” : “zhangsan3”, “age” : 25 }
db.myuser.find({age:{$gt:30}})
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
{ “_id” : ObjectId(“6362360295502fbd56836b99”), “name” : “zhangsan1”, “age” : 58 }
{ “_id” : ObjectId(“6362360295502fbd56836b9a”), “name” : “zhangsan2”, “age” : 68 }
db.myuser.find({age:{$eq:38}})
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
查询多种条件的组合
db.myuser.find( {name: ‘shijiange1’} )
db.myuser.find( {name: ‘shijiange2’} )
db.myuser.find({ $or: [ {name: ‘shijiange1’},{name: ‘shijiange2’} ] })
db.myuser.find({ $and: [ {name: ‘shijiange1’},{age: 20} ] })
db.myuser.find({$or:[{‘name’:’shijiange1′},{‘name’:’shijian2′}]})
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
db.myuser.find({$or:[{‘name’:’shijiange1′},{age: 20}]})
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
mongodb正则查询,支持普通正则和扩展正则
db.myuser.find({ name: {$regex: “shijiange[1-9]”} })#普通正则过滤
db.myuser.find( {“name”:{$regex:”(zhangsan)”}} ) #支持分组正则
db.myuser.find({ name: {$regex: “shijiange[1-9]”} })
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
db.myuser.find({ name: {$regex: “shijiange[2-9]”} })
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
db.myuser.find({name: {$regex: “shijiange[2-9]”}})
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
db.myuser.find({name: {$regex: “(shijiange)”}})
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
db.myuser.find({name: {$regex: “(shijiange+)”}})
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }
{ “_id” : ObjectId(“6362360295502fbd56836b98”), “name” : “shijiange3”, “age” : 38 }
db.myuser.find({name: {$regex: “(shijiange+1)”}})
{ “_id” : ObjectId(“6362360295502fbd56836b96”), “name” : “shijiange1”, “age” : 20 }
db.myuser.find({name: {$regex: “(shijiange+2)”}})
{ “_id” : ObjectId(“6362360295502fbd56836b97”), “name” : “shijiange2”, “age” : 28 }