Nedb db.find()

2018-07-11 19:26 更新

db.find(query, callback)

作用:

查詢符合條件的文檔集。

參數(shù):

query: object類型,查詢條件。支持使用比較運(yùn)算符($lt, $lte, $gt, $gte, $in, $nin, $ne), 邏輯運(yùn)算符($or, $and, $not, $where), 正則表達(dá)式進(jìn)行查詢。

callback(可選): 回調(diào)函數(shù),包含參數(shù)err以及docs,err是報(bào)錯(cuò),docs是查詢到的文檔集。

示例:

// 數(shù)據(jù)存儲(chǔ)集合
 
// { _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: 基本查詢??梢允褂谜齽t表達(dá)式匹配字符串。使用“.”匹配對(duì)象或者數(shù)組里面的元素。
 
// 單字段查詢
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 []
});
 
// 正則表達(dá)式查詢
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
});
 
// 根據(jù)對(duì)象屬性查詢
db.find({ "humans.genders": 2 }, function (err, docs) {
  // docs contains Earth
});
 
// 根據(jù)數(shù)組對(duì)象屬性查詢
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
});
 
 
// 對(duì)象深度比較查詢,不要與"."使用混淆
db.find({ humans: { genders: 2 } }, function (err, docs) {
  // docs is empty, because { genders: 2 } is not equal to { genders: 2, eyes: true }
});
 
// 查詢所有結(jié)果集
db.find({}, function (err, docs) {
});
 
// 查詢某一個(gè)文檔
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代表任意比較運(yùn)算符)
// $lt, $lte: 小于,小于等于
// $gt, $gte: 大于,大于等于
// $in: 屬于
// $ne, $nin: 不等于,不屬于
// $exists: 取值為true或者false,用于檢測(cè)文檔是否具有某一字段
// $regex: 檢測(cè)字符串是否與正則表達(dá)式相匹配
 
// $lt, $lte, $gt and $gte 只能用于數(shù)字和字符串類型
db.find({ "humans.genders": { $gt: 5 } }, function (err, docs) {
  // docs contains Omicron Persei 8, whose humans have more than 5 genders (7).
});
 
// 當(dāng)進(jìn)行字符串比較的時(shí)候,將使用字典序。
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: 當(dāng)文檔中有一個(gè)字段是數(shù)組,NeDB將首先判斷查詢值是否為數(shù)組,如果是數(shù)組的話將執(zhí)行精確查找,然后再去判斷是否存在數(shù)組比較方法(現(xiàn)在只支持$size和$elemMatch)。如果都沒(méi)有,將會(huì)對(duì)所有元素進(jìn)行查詢。
// $size: 匹配數(shù)組的大小
// $elemMatch: 匹配至少一個(gè)數(shù)組元素
 
// 精確查找
db.find({ satellites: ['Phobos', 'Deimos'] }, function (err, docs) {
  // docs contains Mars
})
db.find({ satellites: ['Deimos', 'Phobos'] }, function (err, docs) {
  // docs is empty
})
 
// 使用數(shù)組比較方法
// $elemMatch 運(yùn)算符將匹配數(shù)組中滿足所有條件的元素
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中使用比較運(yùn)算符
db.find({ completeData: { planets: { $elemMatch: { name: 'Earth', number: { $gt: 2 } } } } }, function (err, docs) {
  // docs contains documents with id 5 (completeData)
});
 
// 注意不能使用嵌套的運(yùn)算符, e.g. { $size: { $lt: 5 } } 將會(huì)拋出異常
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: 邏輯運(yùn)算符 $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
// 在第二個(gè)參數(shù)傳入projections對(duì)象,來(lái)規(guī)定返回字段。比如: {a:1, b:1}指定只返回a和b字段,{a:0, b:0}指定省略a和b這兩個(gè)字段。
// _id默認(rèn)返回,不需要返回設(shè)置_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:排序和分頁(yè)
 
// 文檔集
// 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!


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)