【问题标题】:Querying TAFFYDB nested records查询 TAFFYDB 嵌套记录
【发布时间】:2013-11-06 00:11:41
【问题描述】:

我使用 TAFFYDB 创建了一个数据模型。一些字段有嵌套记录。我在查询和更新嵌套记录时遇到困难。

例如:

var friends = TAFFY([
      {
        "id":1,
        "gender":"M",
        "first":"John",
        "last":"Smith",
        "city":"Seattle, WA",
        "comp": 
        [
          {
            "id":1,
            "audience":"cavern"
          },
          {
            "id":2,
            "audience":"cottage"
          }
        ]
      },
      {
        "id":2,
        "gender":"F",
        "first":"Basic",
        "last":"Smith",
        "city":"Seattle, WA",
        "comp": 
        [
          {
            "id":1,
            "audience":"bush"
          },
          {
            "id":2,
            "audience":"swamp"
          }
        ]
      }

    ]);

假设我需要更新任何comp 字段的audience,我该怎么做?

【问题讨论】:

  • 仅供参考,如果您仍然遇到此问题,在 ForerunnerDB 中更新嵌套文档非常容易。这是一个维护良好的现代 JavaScript 数据库,值得一看:github.com/Irrelon/ForerunnerDB

标签: taffydb


【解决方案1】:

关于查询:

当您有更简单的嵌套数组时,您应该能够使用 hashasAll 方法选择特定记录。但是,有一个open issue 指出这些方法都不能正常工作。有提交,但由于问题尚未解决,我认为它们不是 100% 修复的。

对于复杂的嵌套数据,例如您的示例,我发现的唯一内容是this old mailing list conversation 谈论某种查找方法。虽然似乎不存在这样的方法,但文档中也没有提及它。

关于更新:

您应该能够通过将修改后的 JSON 传递到正常更新中来更新“comp”数据(假设您首先能够从数据库中获取数据)。但是,有一个open bug 表明当记录值是对象时更新不起作用。因此,即使您能够查询数据并能够修改它,由于该错误,您无论如何都无法更新记录。但是,您可以执行删除和插入操作。


尽管我发现了上面的内容,但我做了一些测试,发现您可以通过传入对象来更新文件。所以这是一个如何进行简单更新的快速示例:

// To show what TAFFYDB looks like:
console.log(friends().stringify());

"[{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","comp": [{"id":1,"audience":"caven"},{"id":2,"audience":"cottage"}],"___id":"T000003R000002","___s":true},{ "id":2,"gender":"F","first":"Basic","last":"Smith","city":"Seattle, WA","comp":[{"id": 1,"audience":"bush"},{"id":2,"audience":"swamp"}],"___id":"T000003R000003","___s":true}]"

// Get a copy of the comp file from the database for what you want to modify.
// In this example, let's get the **first** record matching people with the name "John Smith":
var johnsComp = friends({first:"John",last:"Smith"}).first().comp;
// Remember, if you want to use select("comp") instead, this will return an array of results.
// So to get the first result, you would need to do this despite there being only one matching result:
// friends({first:"John",last:"Smith"}).select("comp")[0];

// There are no nested queries in TAFFYDB so you need to work with the resulting object as if it were normal javascript.
// You should know the structure and you can either modify things directly, iterate through it, or whatever.
// In this example, I'm just going to change one of the audience values directly:
johnsComp[0].audience = "plains";

// Now let's update that record with the newly modified object.
// Note - if there are more than one "John Smith"s, then all of them will be updated.
friends({first:"John",last:"Smith"}).update({comp:johnsComp});

// To show what TAFFYDB looks like after updating:
console.log(friends().stringify());

"[{"id":1,"gender":"M","first":"John","last":"Smith","city":"Seattle, WA","comp": [{"id":1,"audience":"plains"},{"id":2,"audience":"cottage"}],"___id":"T000003R000002","___s":true},{ "id":2,"gender":"F","first":"Basic","last":"Smith","city":"Seattle, WA","comp":[{"id": 1,"audience":"bush"},{"id":2,"audience":"swamp"}],"___id":"T000003R000003","___s":true}]"

要获得更好的目标查询或更新(可能类似于嵌套查询/更新),您可以尝试传入一个函数。如果你查看docs,这里有一个简单的 update() 示例:

db().update(function () {this.column = "value";return this;}); // sets column to "value" for all matching records

【讨论】:

    【解决方案2】:

    我有一个示例,在这种情况下,我对嵌套字段进行了更新。

    要访问数据,您可以这样做:

    console.log( JSON.stringify( 
        data({'id':'489'}).get()[0].review[0][0].comments
    ))
    

    This is an example how it works

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      相关资源
      最近更新 更多