pouchdb-find( pouchdb查詢擴展插件 ,便於查詢)


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;

	})


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM