如何在Mongo Map/Reduce Emit中将范围变量用作属性名称 [英] How to use scope variables as property names in a Mongo Map/Reduce emit

查看:111
本文介绍了如何在Mongo Map/Reduce Emit中将范围变量用作属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个问题(和答案)处理一般情况.我在将范围变量用作字段键(而不是字段值)方面遇到困难

There is a question (and answer) that deals with the general case. I am having difficulty using a scope variable as a field key (as opposed to the field value)

在下面的示例中,所有FULLY_CAPS字段都是作用域变量.在使用SERVICE和IDENTIFIER的情况下,发射将正确使用范围变量的值,因为它会传递给M/R.

In the example below all the FULLY_CAPS fields are scope variables. In the case of SERVICE and IDENTIFIER the emit correctly uses the value of the scope variable as it is passed to the M/R.

但是,当我尝试使用范围变量的值作为发出的文档中的键时,将使用范围变量名称(而不是其值)来创建文档.

However when I try to use the value of a scope variable as a key in the emitted document, the document is created with the scope variable name (as opposed to it's value).

return emit({
    service: SERVICE,
    date: _this.value.date,
    identifier: _this.value[IDENTIFIER]
  }, {
    errors: {
      count: 1,
      type_breakdown: {
        SINGLES_ONLY: {
          count: 1
        }
      }
    }
  });

有没有办法解决这个问题?

Is there a way around this problem?

推荐答案

在JavaScript中使用快捷方式语法创建对象时,无论引用如何,左侧/属性名称始终被解释为文字值.

When using the shortcut syntax for creating objects in JavaScript, the left hand side/property name is always interpreted as a literal value, regardless of quotes.

例如:

var d={ name: "Aaron" }

等效于:

var d={ "name" : "Aaron" }

有两种方法可以设置属性值:

As there are two ways to set a property value:

  1. obj.propertyName=value
  2. obj["propertName"]=value
  1. obj.propertyName=value
  2. obj["propertName"]=value

您必须至少部分使用第二种语法构造对象.

You have to construct your object using the second syntax, at least in part.

var errors={
      count: 1,
      type_breakdown: { }
      }
    };
var countObj={ count:1 };
errors.type_breakdown[SINGLES_ONLY]=countObj;

// pass results to emit call

这篇关于如何在Mongo Map/Reduce Emit中将范围变量用作属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆