查询 TAFFYDB 嵌套记录



我使用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,我将如何去做?

关于查询:

当您具有更简单的嵌套数组时,您应该能够使用 hashasAll 方法选择特定记录。但是,有一个悬而未决的问题表明这些方法都无法正常工作。有提交,但由于问题一直处于打开状态,我认为它们不是 100% 修复的。

对于复杂的嵌套数据,例如您的示例,我唯一发现的是这个旧的邮件列表对话,讨论某种查找方法。似乎不存在这样的方法,文档中也没有提到它。

关于更新:

您应该能够通过将随之而来的修改后的 JSON(假设您能够首先将数据从数据库中获取)传递到正常更新中来更新"comp"数据。但是,有一个开放的错误显示当记录值是对象时更新不起作用。因此,即使您能够查询数据并能够对其进行修改,由于该错误,您也无法更新记录。但是,您可以进行删除和插入。


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

// 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":"cavern"},{"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}]"

对于更好的目标查询或更新(可能类似于嵌套查询/更新),您可以尝试传入函数。如果你看一下文档,update()有一个简单的例子:

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

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

要访问数据,您可以执行以下操作:

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

这是它如何工作的一个例子

最新更新