第一部分前期准备
1.1插入测试数据
db.test1.insertMany([ {"name":"zhangsan","age":19,"score": [90,80]}, {"name":"lisi","age":29,"score": [40,60]}, {"name":"xiaoming","age":18,"score": [20,40]}, {"name":"xiaohong"} ])
第二部分Find
2.1 Find查询数据的语法格式如下:
>db.collection.find(query,projection) 1.query[可选]: 主要是一些筛选条件, 文档类型数据; 2.projection[可选]: 返回的限制字段, 文档类型.
2.2 find筛选全部数据
第一种,罗列全部数据
> db.test1.find() {"_id":ObjectId("58c8dc54ef9b994487420f29"),"name":"zhangsan","age":19,"score":[ 90, 80 ]} {"_id": ObjectId("58c8dc54ef9b994487420f2a"),"name":"lisi","age":29,"score":[ 40, 60 ]} {"_id": ObjectId("58c8dc54ef9b994487420f2b"),"name":"xiaoming","age":18,"score":[ 20, 40 ]}
第二种筛选某一条数据
> db.test1.find({"name":"xiaoming"}) {"_id": ObjectId("58c8dc54ef9b994487420f2b"),"name":"xiaoming","age":18,"score": [20,40] }
第三种指定返回字段
1或true代表显示,0或false代表不显示
> db.test1.find({"name":"xiaoming"}, {"age":1,"name":1}) {"_id": ObjectId("58c8dc54ef9b994487420f2b"),"name":"xiaoming","age":18}