首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查询TAFFYDB嵌套记录

查询TAFFYDB嵌套记录
EN

Stack Overflow用户
提问于 2013-11-06 00:11:41
回答 2查看 1.8K关注 0票数 4

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

例如:

代码语言:javascript
复制
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,我将如何处理?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-05 16:56:28

关于询问:

当您有更简单的嵌套数组时,您应该能够使用hashasAll方法选择特定的记录。但是,有一个公开发行声明这两个方法都不能正常工作。虽然有提交,但是由于问题一直没有解决,我想它们不是100%固定的。

对于复杂的嵌套数据,就像您的例子一样,我发现的唯一问题是这个古老的邮件列表对话讨论了某种查找方法。然而,似乎没有这样的方法存在,在文档中也没有提到它。

关于最新情况:

您应该能够通过传递修改后的JSON来更新"comp“数据(假设您首先能够将数据从db中取出)进入正常的更新。但是,有一个开虫显示,当记录值是对象时,更新不起作用。因此,即使您能够查询数据并能够修改它,您也不会因为错误而更新记录。但是,您可以执行删除和插入操作。

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

代码语言:javascript
复制
// To show what TAFFYDB looks like:
console.log(friends().stringify());

"[{"id":1,“性别”:“M”,“第一”:“约翰”,“最后”:“史密斯”,“城市”:“西雅图,华盛顿州”,"comp":{"id":1,“观众”:“洞穴”},{"id":2,“观众”:“村舍”},"___id":"T000003R000002","___s":true},{id:2,“性别”:“F”,“第一”:“基本”,“最后”:“史密斯”,“城市”:“西雅图,华盛顿州”,“西雅图”:{“id”:1,“观众”:“布什”},{"id":2,“观众”:“沼泽”},"___id":"T000003R000003",“___s”:真}“

代码语言:javascript
复制
// 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,“性别”:“M”,“第一”:“约翰”,“最后”:“史密斯”,“城市”:“西雅图,华盛顿州”,“西雅图”:“id”:1,“观众”:“平原”},{"id":2,“观众”:“村舍”},"___id":"T000003R000002",“___s”:“___s”,{"id":2,“性别”:“F”,“第一”:“基本”,“最后”:“史密斯”,“城市”:“西雅图,华盛顿州”,“西雅图”:{“id”:1,“观众”:“布什”},{"id":2,“观众”:“沼泽”},"___id":"T000003R000003",“___s”:真}“

为了获得更好的有针对性的查询或更新(类似于嵌套查询/更新),您可以尝试传递一个函数。如果您查看文档,这里有一个简单的更新()示例:

代码语言:javascript
复制
db().update(function () {this.column = "value";return this;}); // sets column to "value" for all matching records
票数 2
EN

Stack Overflow用户

发布于 2016-10-01 15:30:59

我有一个例子,在本例中,我更新了一个嵌套字段。

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

代码语言:javascript
复制
console.log( JSON.stringify( 
    data({'id':'489'}).get()[0].review[0][0].comments
))

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19801647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档