首页 > 代码库 > MongoDB基础教程系列--第五篇 MongoDB 映射与限制记录
MongoDB基础教程系列--第五篇 MongoDB 映射与限制记录
上一篇提到的 find() 的方法,细心的伙伴会发现查询的结果都是显示了集合中全部的字段,实际应用中,显然是不够用的。那么有没有办法指定特定的字段显示出文档呢?答案是肯定的,MongoDB 中用映射实现这种功能。
1、映射
MongoDB 中限制字段的显示,可以利用 0 或 1 来设置字段列表。1 用于显示字段,0 用于隐藏字段。
格式
db.COLLECTION_NAME.find({},{KEY:1})
范例
查询文档时,只显示文档中的 name。首先查询出 user 集合中的所有文档,然后利用映射,返回文档中 name 字段。
> db.user.find(){ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"), "name" : "liruihuan", "age" : 18,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1", "age" : 19,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"), "name" : "user2", "age" : 20,"sex":"woman" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"), "name" : "user3", "age" : 19,"sex":"woman" }>db.user.find({},{"name":1, "_id":0}){ "name" : "liruihuan" }{ "name" : "user1" }{ "name" : "user2" }{ "name" : "user3" }>
如果不加 "_id":0 会返回什么结果呢
> db.user.find({},{"name":1}){ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"), "name" : "liruihuan" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"), "name" : "user2" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"), "name" : "user3" }>
我们发现不设置 "_id":1 结果中也会返回 _id 字段,这是因为在执行 find()
方法时,_id
字段是一直显示的。如果不想显示该字段,则可以设置 "_id":0。
2、限制记录
MongoDB 中想要显示或者跳过指定的文档条数,可以利用 limit() 方法和 skip() 方法
2.1、limit() 方法
limit() 方法接受一个数值类型的参数,其值为想要显示的文档数。
格式
db.COLLECTION_NAME.find().limit(NUMBER)
范例
查询文档时,只显示两条文档。首先查询出 user 集合中的所有文档,然后利用 limit() 方法,显示两条记录。
> db.user.find(){ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"), "name" : "liruihuan", "age" : 18,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1", "age" : 19,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"), "name" : "user2", "age" : 20,"sex":"woman" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"), "name" : "user3", "age" : 19,"sex":"woman" }>db.user.find().limit(2){ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"), "name" : "liruihuan", "age" : 18,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1", "age" : 19,"sex":"man" }>
如果不给 limit() 指定参数呢,我们会发现,返回了全部文档。
> db.user.find().limit(){ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"), "name" : "liruihuan", "age" : 18,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1", "age" : 19,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"), "name" : "user2", "age" : 20,"sex":"woman" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"), "name" : "user3", "age" : 19,"sex":"woman" }
2.2、skip() 方法
skip() 方法接受一个数值类型的参数,其值为想要跳过的文档数。
格式
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
范例
查询文档时,只显示第二条文档。首先查询出 user 集合中的所有文档,然后利用 limit(1) 方法,显示一条文档,利用 skip(1) 方法跳过第一条文档。
> db.user.find(){ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"), "name" : "liruihuan", "age" : 18,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1", "age" : 19,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"), "name" : "user2", "age" : 20,"sex":"woman" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"), "name" : "user3", "age" : 19,"sex":"woman" }>db.user.find().limit(1).skip(1){ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1", "age" : 19,"sex":"man" }>
skip() 方法的默认值是 0 。
> db.user.find(){ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"), "name" : "liruihuan", "age" : 18,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1", "age" : 19,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"), "name" : "user2", "age" : 20,"sex":"woman" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"), "name" : "user3", "age" : 19,"sex":"woman" }> db.user.find().skip(){ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754b"), "name" : "liruihuan", "age" : 18,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754d"), "name" : "user1", "age" : 19,"sex":"man" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754e"), "name" : "user2", "age" : 20,"sex":"woman" }{ "_id" : ObjectId("58e1d2f0bb1bbc3245fa754f"), "name" : "user3", "age" : 19,"sex":"woman" }>
业精于勤,荒于嬉;行成于思,毁于随。
如果你觉得这篇文章不错或者对你有所帮助,可以通过右侧【打赏】功能,给予博主一点点鼓励和支持
MongoDB基础教程系列--第五篇 MongoDB 映射与限制记录