Nedb db.find()

2018-07-11 19:26 更新

db.find(query, callback)

作用:

查询符合条件的文档集。

参数:

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

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

示例:

// 数据存储集合
 
// { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false, satellites: ['Phobos', 'Deimos'] }
// { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true, humans: { genders: 2, eyes: true } }
// { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false }
// { _id: 'id4', planet: 'Omicron Persei 8', system: 'futurama', inhabited: true, humans: { genders: 7 } }
// { _id: 'id5', completeData: { planets: [ { name: 'Earth', number: 3 }, { name: 'Mars', number: 2 }, { name: 'Pluton', number: 9 } ] } }
 
// 示例1: 基本查询。可以使用正则表达式匹配字符串。使用“.”匹配对象或者数组里面的元素。
 
// 单字段查询
db.find({ system: 'solar' }, function (err, docs) {
  // docs is an array containing documents Mars, Earth, Jupiter
  // If no document is found, docs is equal to []
});
 
// 正则表达式查询
db.find({ planet: /ar/ }, function (err, docs) {
  // docs contains Mars and Earth
});
 
// 多条件查询
db.find({ system: 'solar', inhabited: true }, function (err, docs) {
  // docs is an array containing document Earth only
});
 
// 根据对象属性查询
db.find({ "humans.genders": 2 }, function (err, docs) {
  // docs contains Earth
});
 
// 根据数组对象属性查询
db.find({ "completeData.planets.name": "Mars" }, function (err, docs) {
  // docs contains document 5
});
 
db.find({ "completeData.planets.name": "Jupiter" }, function (err, docs) {
  // docs is empty
});
 
db.find({ "completeData.planets.0.name": "Earth" }, function (err, docs) {
  // docs contains document 5
  // If we had tested against "Mars" docs would be empty because we are matching against a specific array element
});
 
 
// 对象深度比较查询,不要与"."使用混淆
db.find({ humans: { genders: 2 } }, function (err, docs) {
  // docs is empty, because { genders: 2 } is not equal to { genders: 2, eyes: true }
});
 
// 查询所有结果集
db.find({}, function (err, docs) {
});
 
// 查询某一个文档
db.findOne({ _id: 'id1' }, function (err, doc) {
  // doc is the document Mars
  // If no document is found, doc is null
});
 
 
// 示例2: {field: {$op: value}} ($op代表任意比较运算符)
// $lt, $lte: 小于,小于等于
// $gt, $gte: 大于,大于等于
// $in: 属于
// $ne, $nin: 不等于,不属于
// $exists: 取值为true或者false,用于检测文档是否具有某一字段
// $regex: 检测字符串是否与正则表达式相匹配
 
// $lt, $lte, $gt and $gte 只能用于数字和字符串类型
db.find({ "humans.genders": { $gt: 5 } }, function (err, docs) {
  // docs contains Omicron Persei 8, whose humans have more than 5 genders (7).
});
 
// 当进行字符串比较的时候,将使用字典序。
db.find({ planet: { $gt: 'Mercury' }}, function (err, docs) {
  // docs contains Omicron Persei 8
})
 
// Using $in. $nin is used in the same way
db.find({ planet: { $in: ['Earth', 'Jupiter'] }}, function (err, docs) {
  // docs contains Earth and Jupiter
});
 
// Using $exists
db.find({ satellites: { $exists: true } }, function (err, docs) {
  // docs contains only Mars
});
 
// Using $regex with another operator
db.find({ planet: { $regex: /ar/, $nin: ['Jupiter', 'Earth'] } }, function (err, docs) {
  // docs only contains Mars because Earth was excluded from the match by $nin
});
 
 
// 示例3: 当文档中有一个字段是数组,NeDB将首先判断查询值是否为数组,如果是数组的话将执行精确查找,然后再去判断是否存在数组比较方法(现在只支持$size和$elemMatch)。如果都没有,将会对所有元素进行查询。
// $size: 匹配数组的大小
// $elemMatch: 匹配至少一个数组元素
 
// 精确查找
db.find({ satellites: ['Phobos', 'Deimos'] }, function (err, docs) {
  // docs contains Mars
})
db.find({ satellites: ['Deimos', 'Phobos'] }, function (err, docs) {
  // docs is empty
})
 
// 使用数组比较方法
// $elemMatch 运算符将匹配数组中满足所有条件的元素
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: 3 } } } }, function (err, docs) {
  // docs contains documents with id 5 (completeData)
});
 
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: 5 } } } }, function (err, docs) {
  // docs is empty
});
 
// 在$elemMatch中使用比较运算符
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: { $gt: 2 } } } } }, function (err, docs) {
  // docs contains documents with id 5 (completeData)
});
 
// 注意不能使用嵌套的运算符, e.g. { $size: { $lt: 5 } } 将会抛出异常
db.find({ satellites: { $size: 2 } }, function (err, docs) {
  // docs contains Mars
});
 
db.find({ satellites: { $size: 1 } }, function (err, docs) {
  // docs is empty
});
 
// If a document's field is an array, matching it means matching any element of the array
db.find({ satellites: 'Phobos' }, function (err, docs) {
  // docs contains Mars. Result would have been the same if query had been { satellites: 'Deimos' }
});
 
// This also works for queries that use comparison operators
db.find({ satellites: { $lt: 'Amos' } }, function (err, docs) {
  // docs is empty since Phobos and Deimos are after Amos in lexicographical order
});
 
// This also works with the $in and $nin operator
db.find({ satellites: { $in: ['Moon', 'Deimos'] } }, function (err, docs) {
  // docs contains Mars (the Earth document is not complete!)
});
 
// 示例4: 逻辑运算符 $or, $and, $not, $where
// $or, $and: 并集,交集 { $op: [query1, query2, ...] }
// $not: 取非 { $not: query }
// $where: 条件 { $where: function () { /* object is "this", return a boolean */ } }
 
db.find({ $or: [{ planet: 'Earth' }, { planet: 'Mars' }] }, function (err, docs) {
  // docs contains Earth and Mars
});
 
db.find({ $not: { planet: 'Earth' } }, function (err, docs) {
  // docs contains Mars, Jupiter, Omicron Persei 8
});
 
db.find({ $where: function () { return Object.keys(this) > 6; } }, function (err, docs) {
  // docs with more than 6 properties
});
 
// You can mix normal queries, comparison queries and logical operators
db.find({ $or: [{ planet: 'Earth' }, { planet: 'Mars' }], inhabited: true }, function (err, docs) {
  // docs contains Earth
});
 
// 示例5: Projections
// 在第二个参数传入projections对象,来规定返回字段。比如: {a:1, b:1}指定只返回a和b字段,{a:0, b:0}指定省略a和b这两个字段。
// _id默认返回,不需要返回设置_id: 0
 
// Same database as above
 
// Keeping only the given fields
db.find({ planet: 'Mars' }, { planet: 1, system: 1 }, function (err, docs) {
  // docs is [{ planet: 'Mars', system: 'solar', _id: 'id1' }]
});
 
// Keeping only the given fields but removing _id
db.find({ planet: 'Mars' }, { planet: 1, system: 1, _id: 0 }, function (err, docs) {
  // docs is [{ planet: 'Mars', system: 'solar' }]
});
 
// Omitting only the given fields and removing _id
db.find({ planet: 'Mars' }, { planet: 0, system: 0, _id: 0 }, function (err, docs) {
  // docs is [{ inhabited: false, satellites: ['Phobos', 'Deimos'] }]
});
 
// Failure: using both modes at the same time
db.find({ planet: 'Mars' }, { planet: 0, system: 1 }, function (err, docs) {
  // err is the error message, docs is undefined
});
 
// You can also use it in a Cursor way but this syntax is not compatible with MongoDB
db.find({ planet: 'Mars' }).projection({ planet: 1, system: 1 }).exec(function (err, docs) {
  // docs is [{ planet: 'Mars', system: 'solar', _id: 'id1' }]
});
 
// Project on a nested document
db.findOne({ planet: 'Earth' }).projection({ planet: 1, 'humans.genders': 1 }).exec(function (err, doc) {
  // doc is { planet: 'Earth', _id: 'id2', humans: { genders: 2 } }
});
 
// 示例6:排序和分页
 
// 文档集
// doc1 = { _id: 'id1', planet: 'Mars', system: 'solar', inhabited: false, satellites: ['Phobos', 'Deimos'] }
// doc2 = { _id: 'id2', planet: 'Earth', system: 'solar', inhabited: true, humans: { genders: 2, eyes: true } }
// doc3 = { _id: 'id3', planet: 'Jupiter', system: 'solar', inhabited: false }
// doc4 = { _id: 'id4', planet: 'Omicron Persei 8', system: 'futurama', inhabited: true, humans: { genders: 7 } }
 
// No query used means all results are returned (before the Cursor modifiers)
db.find({}).sort({ planet: 1 }).skip(1).limit(2).exec(function (err, docs) {
  // docs is [doc3, doc1]
});
 
// You can sort in reverse order like this
db.find({ system: 'solar' }).sort({ planet: -1 }).exec(function (err, docs) {
  // docs is [doc1, doc3, doc2]
});
 
// You can sort on one field, then another, and so on like this:
db.find({}).sort({ firstField: 1, secondField: -1 }) ...   // You understand how this works!


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号