MongoDB 查询文档(一)

来源:互联网 发布:https的默认端口是 编辑:程序博客网 时间:2024/05/21 13:55
  

第一部分 前期准备

1.1 插入测试数据

db.test1.insertMany([     {"name""zhangsan""age"19"score": [9080]},     {"name""lisi""age"29,"score": [4060]},     {"name""xiaoming""age"18"score": [2040]},     {"name""xiaohong"} ])

第二部分 Find

2.1 Find查询数据的语法格式如下:

db.collection.find(queryprojection) 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" : [ 2040 ] }

第三种 指定返回字段

1或true代表显示, 0或false代表不显示

> db.test1.find({"name""xiaoming"}, {"age"1"name"1}) { "_id" : ObjectId("58c8dc54ef9b994487420f2b"), "name" : "xiaoming""age" : 18 }

 

 

0 0