pouchdb-find
pouchdb-find
環境搭建
下載lib
bower install pouchdb-find
引入js
<script src="pouchdb.js"></script>
<script src="pouchdb.find.js"></script>
使用
1.createIndex
db.createIndex({
index: {
fields: ['foo']
}
}).then(function (result) {
// yo, a result
}).catch(function (err) {
// ouch, an error
});
2.find
db.find({
selector: {name: 'Mario'},
fields: ['_id', 'name'],
sort: ['name']
}).then(function (result) {
// yo, a result
}).catch(function (err) {
// ouch, an error
});
demo
db.createIndex({
index: {fields: ['name']}
}).then(function () {
return db.find({
selector: {name: {$gt: null}},
sort: ['name']
});
});
example Result
{
"docs": [
{
"_id": "mario",
"name": "Mario"
}
]
}
find 查詢語法
為了避免出現bug 在每個查詢都添加_id: {$gte: null}
(可以設為null意外的值)
db.find({
selector: {
_id: {$gte: null},
name: {$eq: 'Mario'}
}
});
條件關鍵詞解析
- $lt Match fields "less than" this one.
- $gt Match fields "greater than" this one.
- $lte Match fields "less than or equal to" this one.
- $gte Match fields "greater than or equal to" this one.
- $eq Match fields equal to this one.
- $ne Match fields not equal to this one.
- $exists True if the field should exist, false otherwise.
- $type One of: "null", "boolean", "number", "string", "array", or "object".
- $regex use RegExp
相等查詢
//1.
db.find({
selector: {
_id: {$gte: null},
name: {$eq: 'Mario'}
}
});
//2.
db.find({
selector: {
_id: {$gte: null},
name: 'Mario'
}
});
多條件查詢
//1.
db.find({
selector: {
_id: {$gte: null},
series: 'Mario',
debut: { $gt: 1990 }
}
});
//2.
db.find({
selector: {
$and: [
_id: {$gte: null},
{ series: 'Mario' },
{ debut: { $gt: 1990 } }
]
}
});
模糊查詢$regex
var keyword = 'mik'
var regExp = new RegExp('.*' + keyword + '.*', 'i');
db.find({
selector:{
_id: {"$gte": "NOB", "$lte": 'NOE'},
name:{"$regex": regExp}
}
})
.then(function(result){
//do some thing
var results = result.docs;
})