3、db.find(query, callback)

作用:

查询符合条件的文档集。

参数:

query: object 类型,查询条件。支持使用比较运算符 ($lt, $lte, $gt, $gte, $in, $nin, $ne), 逻辑运算符 ($or, $and, $not, $where), 正则表达式进行查询。

callback(可选): 回调函数,包含参数 err 以及 docs,err 是报错,docs 是查询到的文档集。

示例:

  1. // 数据存储集合
  2. // { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false, satellites: ['Phobos', 'Deimos'] }
  3. // { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true, humans: { genders: 2, eyes: true } }
  4. // { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false }
  5. // { _id: 'id4', planet: 'Omicron Persei 8', system: 'futurama', inhabited: true, humans: { genders: 7 } }
  6. // { _id: 'id5', completeData: { planets: [ { name: 'Earth', number: 3 }, { name: 'Mars', number: 2 }, { name: 'Pluton', number: 9 } ] } }
  7. // 示例1: 基本查询。可以使用正则表达式匹配字符串。使用“.”匹配对象或者数组里面的元素。
  8. // 单字段查询
  9. db.find({system:'solar'},function(err,docs){
  10. // docs is an array containing documents Mars, Earth, Jupiter
  11. // If no document is found, docs is equal to []
  12. });
  13. // 正则表达式查询
  14. db.find({planet:/ar/},function(err,docs){
  15. // docs contains Mars and Earth
  16. });
  17. // 多条件查询
  18. db.find({system:'solar',inhabited:true},function(err,docs){
  19. // docs is an array containing document Earth only
  20. });
  21. // 根据对象属性查询
  22. db.find({"humans.genders":2},function(err,docs){
  23. // docs contains Earth
  24. });
  25. // 根据数组对象属性查询
  26. db.find({"completeData.planets.name":"Mars"},function(err,docs){
  27. // docs contains document 5
  28. });
  29. db.find({"completeData.planets.name":"Jupiter"},function(err,docs){
  30. // docs is empty
  31. });
  32. db.find({"completeData.planets.0.name":"Earth"},function(err,docs){
  33. // docs contains document 5
  34. // If we had tested against "Mars" docs would be empty because we are matching against a specific array element
  35. });
  36. // 对象深度比较查询,不要与"."使用混淆
  37. db.find({humans:{genders:2}},function(err,docs){
  38. // docs is empty, because { genders: 2 } is not equal to { genders: 2, eyes: true }
  39. });
  40. // 查询所有结果集
  41. db.find({},function(err,docs){
  42. });
  43. // 查询某一个文档
  44. db.findOne({_id:'id1'},function(err,doc){
  45. // doc is the document Mars
  46. // If no document is found, doc is null
  47. });
  48. // 示例2: {field: {$op: value}} ($op代表任意比较运算符)
  49. // $lt, $lte: 小于,小于等于
  50. // $gt, $gte: 大于,大于等于
  51. // $in: 属于
  52. // $ne, $nin: 不等于,不属于
  53. // $exists: 取值为true或者false,用于检测文档是否具有某一字段
  54. // $regex: 检测字符串是否与正则表达式相匹配
  55. // $lt, $lte, $gt and $gte 只能用于数字和字符串类型
  56. db.find({"humans.genders":{$gt:5}},function(err,docs){
  57. // docs contains Omicron Persei 8, whose humans have more than 5 genders (7).
  58. });
  59. // 当进行字符串比较的时候,将使用字典序。
  60. db.find({planet:{$gt:'Mercury'}},function(err,docs){
  61. // docs contains Omicron Persei 8
  62. })
  63. // Using $in. $nin is used in the same way
  64. db.find({planet:{$in:['Earth','Jupiter']}},function(err,docs){
  65. // docs contains Earth and Jupiter
  66. });
  67. // Using $exists
  68. db.find({satellites:{$exists:true}},function(err,docs){
  69. // docs contains only Mars
  70. });
  71. // Using $regex with another operator
  72. db.find({planet:{$regex:/ar/,$nin:['Jupiter','Earth']}},function(err,docs){
  73. // docs only contains Mars because Earth was excluded from the match by $nin
  74. });
  75. // 示例3: 当文档中有一个字段是数组,NeDB将首先判断查询值是否为数组,如果是数组的话将执行精确查找,然后再去判断是否存在数组比较方法(现在只支持$size和$elemMatch)。如果都没有,将会对所有元素进行查询。
  76. // $size: 匹配数组的大小
  77. // $elemMatch: 匹配至少一个数组元素
  78. // 精确查找
  79. db.find({satellites:['Phobos','Deimos']},function(err,docs){
  80. // docs contains Mars
  81. })
  82. db.find({satellites:['Deimos','Phobos']},function(err,docs){
  83. // docs is empty
  84. })
  85. // 使用数组比较方法
  86. // $elemMatch 运算符将匹配数组中满足所有条件的元素
  87. db.find({completeData:{planets:{$elemMatch:{name:'Earth',number:3}}}},function(err,docs){
  88. // docs contains documents with id 5 (completeData)
  89. });
  90. db.find({completeData:{planets:{$elemMatch:{name:'Earth',number:5}}}},function(err,docs){
  91. // docs is empty
  92. });
  93. // 在$elemMatch中使用比较运算符
  94. db.find({completeData:{planets:{$elemMatch:{name:'Earth',number:{$gt:2}}}}},function(err,docs){
  95. // docs contains documents with id 5 (completeData)
  96. });
  97. // 注意不能使用嵌套的运算符, e.g. { $size: { $lt: 5 } } 将会抛出异常
  98. db.find({satellites:{$size:2}},function(err,docs){
  99. // docs contains Mars
  100. });
  101. db.find({satellites:{$size:1}},function(err,docs){
  102. // docs is empty
  103. });
  104. // If a document's field is an array, matching it means matching any element of the array
  105. db.find({satellites:'Phobos'},function(err,docs){
  106. // docs contains Mars. Result would have been the same if query had been { satellites: 'Deimos' }
  107. });
  108. // This also works for queries that use comparison operators
  109. db.find({satellites:{$lt:'Amos'}},function(err,docs){
  110. // docs is empty since Phobos and Deimos are after Amos in lexicographical order
  111. });
  112. // This also works with the $in and $nin operator
  113. db.find({satellites:{$in:['Moon','Deimos']}},function(err,docs){
  114. // docs contains Mars (the Earth document is not complete!)
  115. });
  116. // 示例4: 逻辑运算符 $or, $and, $not, $where
  117. // $or, $and: 并集,交集 { $op: [query1, query2, ...] }
  118. // $not: 取非 { $not: query }
  119. // $where: 条件 { $where: function () { /* object is "this", return a boolean */ } }
  120. db.find({$or:[{planet:'Earth'},{planet:'Mars'}]},function(err,docs){
  121. // docs contains Earth and Mars
  122. });
  123. db.find({$not:{planet:'Earth'}},function(err,docs){
  124. // docs contains Mars, Jupiter, Omicron Persei 8
  125. });
  126. db.find({$where:function(){returnObject.keys(this)>6;}},function(err,docs){
  127. // docs with more than 6 properties
  128. });
  129. // You can mix normal queries, comparison queries and logical operators
  130. db.find({$or:[{planet:'Earth'},{planet:'Mars'}],inhabited:true},function(err,docs){
  131. // docs contains Earth
  132. });
  133. // 示例5: Projections
  134. // 在第二个参数传入projections对象,来规定返回字段。比如: {a:1, b:1}指定只返回a和b字段,{a:0, b:0}指定省略a和b这两个字段。
  135. // _id默认返回,不需要返回设置_id: 0
  136. // Same database as above
  137. // Keeping only the given fields
  138. db.find({planet:'Mars'},{planet:1,system:1},function(err,docs){
  139. // docs is [{ planet: 'Mars', system: 'solar', _id: 'id1' }]
  140. });
  141. // Keeping only the given fields but removing _id
  142. db.find({planet:'Mars'},{planet:1,system:1,_id:0},function(err,docs){
  143. // docs is [{ planet: 'Mars', system: 'solar' }]
  144. });
  145. // Omitting only the given fields and removing _id
  146. db.find({planet:'Mars'},{planet:0,system:0,_id:0},function(err,docs){
  147. // docs is [{ inhabited: false, satellites: ['Phobos', 'Deimos'] }]
  148. });
  149. // Failure: using both modes at the same time
  150. db.find({planet:'Mars'},{planet:0,system:1},function(err,docs){
  151. // err is the error message, docs is undefined
  152. });
  153. // You can also use it in a Cursor way but this syntax is not compatible with MongoDB
  154. db.find({planet:'Mars'}).projection({planet:1,system:1}).exec(function(err,docs){
  155. // docs is [{ planet: 'Mars', system: 'solar', _id: 'id1' }]
  156. });
  157. // Project on a nested document
  158. db.findOne({planet:'Earth'}).projection({planet:1,'humans.genders':1}).exec(function(err,doc){
  159. // doc is { planet: 'Earth', _id: 'id2', humans: { genders: 2 } }
  160. });
  161. // 示例6:排序和分页
  162. // 文档集
  163. // doc1 = { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false, satellites: ['Phobos', 'Deimos'] }
  164. // doc2 = { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true, humans: { genders: 2, eyes: true } }
  165. // doc3 = { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false }
  166. // doc4 = { _id: 'id4', planet: 'Omicron Persei 8', system: 'futurama', inhabited: true, humans: { genders: 7 } }
  167. // No query used means all results are returned (before the Cursor modifiers)
  168. db.find({}).sort({planet:1}).skip(1).limit(2).exec(function(err,docs){
  169. // docs is [doc3, doc1]
  170. });
  171. // You can sort in reverse order like this
  172. db.find({system:'solar'}).sort({planet:-1}).exec(function(err,docs){
  173. // docs is [doc1, doc3, doc2]
  174. });
  175. // You can sort on one field, then another, and so on like this:
  176. db.find({}).sort({firstField:1,secondField:-1})... // You understand how this works!