4

I have created a data model using TAFFYDB. Some of the fields have nested records. I am facing difficulties querying and updating the nested records.

For example:

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"
          }
        ]
      }

    ]);

Supposing I need to update any of the comp field's audience, how will I go about it?

4

2 回答 2

2

关于查询:

当您有更简单的嵌套数组时,您应该能够使用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":"洞穴"},{"id":2,"audience":"cottage"}],"___id":"T000003R000002","___s":true},{"id" :2,"gender":"F","first":"Basic","last":"Smith","city":"Seattle, WA","comp":[{"id":1,"观众":"布什"},{"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,"观众":"布什"},{"id":2,"audience":"swamp"}],"___id":"T000003R000003","___s":true}]"

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

db().update(function () {this.column = "value";return this;}); // sets column to "value" for all matching records
于 2014-06-05T16:56:28.487 回答
0

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

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

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

这是一个如何工作的例子

于 2016-10-01T15:30:59.047 回答