gpt4 book ai didi

pouchdb - 使用参数进行PouchDB查询

转载 作者:行者123 更新时间:2023-12-04 13:39:56 24 4
gpt4 key购买 nike

假设我们将以JSON对象表示的汽车(约40MB)存储在PouchDB中,并且我们想基于马力属性进行搜索。 sql中的示例:从HP> 100的汽车中选择*。

您可以按键查询pouchDB,但是显然HP并不是文档的键。有没有办法可以做到这一点?

据我了解的 map 功能,

function(doc) {
if(doc.value) {
emit(doc.value, null);
}
}

在函数的外部范围内不能访问任何变量。
var horsePower = $scope.horsePowerInputField

function(doc) {
if(doc.hp > horsePower) {
emit(doc.value, null);
}
}

那么有可能查询基于非关键变量参数化的数据库吗?

最佳答案

PouchDB 2.0.0开始,支持map/reduce查询中的闭包。 Details here

但是,如果可能的话,您应该避免使用它们,因为

  • CouchDB不支持它们,只有PouchDB不支持
  • Saved map/reduce views更快,可能会在2.1.0中添加,但不支持闭包。

  • 话虽如此,如果您确实想使用闭包,现在可以执行以下操作:
    var horsePower = $scope.horsePowerInputField

    function(doc, emit) { // extra 'emit' tells PouchDB to allow closures
    if(doc.hp > horsePower) {
    emit(doc.value, null);
    }
    }

    关于pouchdb - 使用参数进行PouchDB查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20729975/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com