标签:
> db.user.find().pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}
{
"_id" : ObjectId("5694ce6685cf2632ea951c05"),
"name" : "xiaoniao",
"password" : "lijian",
"grade" : "15"
}
skip()默认的参数是0;
> db.user.find().limit(2).pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}
> db.user.find().limit(1).pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
> db.user.find().limit(1).skip(2).pretty()
{
"_id" : ObjectId("5694ce6685cf2632ea951c05"),
"name" : "xiaoniao",
"password" : "lijian",
"grade" : "15"
}
插入多条数据方便下边排序:
> db.user.save({name:"张三",password:"zhangsan",grade:60},{name:"李四",password:
"lisi",grade:75},{name:"毛毛",password:"maoling",grade:99})
WriteResult({ "nInserted" : 1 })
发现重大问题,mongo不支持一句插入多条数据,多条数据只能一句一句来插入,所以以上语句分次插入
> db.user.find().pretty()
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}
{
"_id" : ObjectId("5694ce6685cf2632ea951c05"),
"name" : "xiaoniao",
"password" : "lijian",
"grade" : "15"
}
{
"_id" : ObjectId("5694d84c85cf2632ea951c06"),
"name" : "张三",
"password" : "zhangsan",
"grade" : 60
}
{
"_id" : ObjectId("5694dab385cf2632ea951c07"),
"name" : "李四",
"password" : "lisi",
"grade" : 80
}
{
"_id" : ObjectId("5694dae885cf2632ea951c08"),
"name" : "毛毛",
"password" : "maomao",
"grade" : 100
}
在MongoDB中使用使用sort()方法对数据进行排序,sort()方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而-1是用于降序排列。
> db.user.find().sort({"grade":-1}).pretty()
{
"_id" : ObjectId("5694ce6685cf2632ea951c05"),
"name" : "xiaoniao",
"password" : "lijian",
"grade" : "15"
}
{
"_id" : ObjectId("5694dae885cf2632ea951c08"),
"name" : "毛毛",
"password" : "maomao",
"grade" : 100
}
{
"_id" : ObjectId("5694714a3a18f4867aecbcd2"),
"name" : "aaaaaaaaa",
"password" : "123456",
"grade" : 99
}
{
"_id" : ObjectId("5694dab385cf2632ea951c07"),
"name" : "李四",
"password" : "lisi",
"grade" : 80
}
{
"_id" : ObjectId("5694d84c85cf2632ea951c06"),
"name" : "张三",
"password" : "zhangsan",
"grade" : 60
}
{
"_id" : ObjectId("56946fba3a18f4867aecbcd1"),
"name" : "admin",
"password" : "123456",
"title" : "admin",
"grade" : 50
}
学习mongo系列(六)limit(munber),skip(number)
标签:
原文地址:http://www.cnblogs.com/mxh1099/p/5125281.html