【问题标题】:Parse Cloud Code syntax differences causing failed query解析导致查询失败的 Cloud Code 语法差异
【发布时间】:2019-01-31 02:57:43
【问题描述】:

我们需要一个使用多个阶段的聚合管道,例如:addFieldslookupgroupunwind。将 MongoDB 罗盘中的工作语法转换为解析云代码 javascript 调用时,我们没有得到相同的结果。

从 MongoDB 指南针导出的“节点”代码如下:

[
  {
    '$addFields': {
      'user': {
        '$substr': [
          '$_p_pUser', 6, -1
        ]
      }
    }
  }, {
    '$lookup': {
      'from': '_User', 
      'localField': 'user', 
      'foreignField': '_id', 
      'as': 'userobject'
    }
  }, {
    '$addFields': {
      'username': '$userobject.username'
    }
  }, {
    '$unwind': {
      'path': '$username'
    }
  }, {
    '$group': {
      '_id': '$username', 
      'total': {
        '$sum': '$score'
      }
    }
  }
]

原始 mongoDB 查询与解析服务器调用所需的语法之间存在一些显着差异。转换为应该工作的语法(据我所知)如下:

var pipeline = 
    {
        addFields : 
        { 
            user : '$pUser.objectId', // different from the above syntax. This is how the object id of the user is accessed
            username: '$userobject.username'
        },
        lookup : {
            from: '_User',
            localField: 'user',
            foreignField: '_id', // although most calls from the parse server aggregate pipeline should access "_is" instead by "objectId", in this case it seems the lookup table has a field name "_id" and not "objectId"
            as: 'userobject'
        },
        unwind : { path: '$username' },
        group : {
            objectId: '$username',
            total : {
              $sum : '$score'
            }
        }
    };
    var pipelineResults = await gameTableQuery.aggregate(pipeline);

预期输出

预期的输出是将当前表的用户指针 (pUser) 与用户表匹配。然后按用户名分组,同时找到分数的总和。

实际输出

在上述情况下,在查找阶段搜索_id时,返回了0个条目。

在查找阶段 foreignField 被替换为 objectId 的情况下,每个用户而不是仅匹配本地字段的用户。

lookup : {
    from: '_User',
    localField: 'user',
    foreignField: 'objectId',
    as: 'userobject'
}

似乎发生了一些语法错误,在 MongoDB 语法和解析服务器聚合管道语法之间的转换中丢失了。

【问题讨论】:

    标签: javascript mongodb aggregation-framework parse-server


    【解决方案1】:

    你能写一个失败的测试吗

    fit('order by pointer', (done) => {
        const pointer1 = new TestObject({ value: 1});
        const pointer2 = new TestObject({ value: 2});
        const pointer3 = new TestObject({ value: 3});
    
        const obj1 = new TestObject({ pointer: pointer1 });
        const obj2 = new TestObject({ pointer: pointer2 });
        const obj3 = new TestObject({ pointer: pointer3 });
        const pipeline = [
          {
            project: {
              tempPointer: { $substr: [ "$_p_pointer", 11, -1 ] }, // Remove TestObject$
            }
          },
          {
            lookup: {
              from: "test_TestObject",
              localField: "tempPointer",
              foreignField: "_id",
              as: "tempPointer"
            }
          },
          {
            unwind: {
              path: "$tempPointer",
            }
          },
          { sort : { "tempPointer.value" : -1 } }
        ];
        Parse.Object.saveAll([pointer1, pointer2, pointer3, obj1, obj2, obj3]).then(() => {
          const query = new Parse.Query(TestObject);
          return query.aggregate(pipeline);
        }).then((results) => {
          expect(results.length).toEqual(3);
          expect(results[0].tempPointer.value).toEqual(3);
          expect(results[1].tempPointer.value).toEqual(2);
          expect(results[2].tempPointer.value).toEqual(1);
          console.log(results);
          done();
        }).catch((error) => {
          console.log(error);
          done();
        });
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      相关资源
      最近更新 更多