pouchdb-find( pouchdb查询扩展插件 ,便于查询)

本文介绍了PouchDB-find插件的安装与使用方法,包括环境搭建、索引创建及查询操作等内容。提供了详细的示例代码,展示了如何进行条件筛选、排序及模糊查询等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

    })

转载于:https://www.cnblogs.com/final-elysion/p/6096995.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值