rethinkdb - 根据父级中的字段创建包含数组的新字段

时间:2015-08-01 21:06:08

标签: rethinkdb

使用公共数据集我在表上运行此查询:

r.table("contacts") .filter({"Type": "Agent","ContactDescription" : "CONDO"}) .hasFields("CorporationName") .group("CorporationName") .ungroup() .merge(function(row){ return {count: row('reduction').count()}; }) .orderBy(r.desc('count'))

产生这个输出:

{
    "count": 167,
    "group": "THE ANDREWS ORGANIZATION",
    "reduction": [
        {
            "BusinessApartment": "12",
            "BusinessCity": "NEW YORK",
            "BusinessHouseNumber": "666",
            "BusinessState": "NY",
            "BusinessStreetName": "BROADWAY",
            "BusinessZip": "10012",
            "ContactDescription": "CONDO",
            "CorporationName": "THE ANDREWS ORGANIZATION",
            "FirstName": "EUGENE",
            "LastName": "ANDREWS",
            "RegistrationContactID": "37946804",
            "RegistrationID": "379468",
            "Type": "Agent",
            "id": "09a89cc3-67ee-4a95-b8db-cd118cb5e40a"
        },
        {
            "BusinessApartment": "12",
            "BusinessCity": "NEW YORK",
            "BusinessHouseNumber": "666",
            "BusinessState": "NY",
            "BusinessStreetName": "BROADWAY",
            "BusinessZip": "10012",
            "ContactDescription": "CONDO",
            "CorporationName": "THE ANDREWS ORGANIZATION",
            "FirstName": "EUGENE",
            "LastName": "ANDREWS",
            "RegistrationContactID": "14378404",
            "RegistrationID": "143784",
            "Type": "Agent",
            "id": "23fbb5ca-b66c-4784-893c-e60752f9386d"
        },

如何根据此查询修改查询,以便在名为“siblings”的每个对象中创建一个新字段(数组):

r.table("contacts").getAll(reduction.RegistrationID, {index: "RegistrationID"})

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

r.table("contacts")
 .filter({"Type": "Agent","ContactDescription" : "CONDO"})
 .hasFields("CorporationName") 
 .group("CorporationName") 
 .ungroup() 
 .merge(function(row){ return {count: row('reduction').count()}; })  
 // Start Query
 // Map over every single row/result and merge a `reduction` property
 .merge(function (row) {
    return {
      // Map over every object in the reduction
      'reduction': row('reduction').map(function (obj) {
        // Add a `siblings` property to the object
        return obj.merge({
          // Execute your query and coerce it to an array
          'siblings': r.table("contacts")
            .getAll(obj('RegistrationID'), {index: "RegistrationID"}).coerceTo('array')
        })
      })
    }
  })
 // End
 .orderBy(r.desc('count'))