在 MongoDB 的单个字段中连接数组中的字符串值 [英] Concatenate string values in array in a single field in MongoDB

查看:40
本文介绍了在 MongoDB 的单个字段中连接数组中的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一系列格式如下的文档:

Suppose that I have a series of documents with the following format:

{
    "_id": "3_0",
    "values": ["1", "2"]
}

并且我想获得连接在单个字段中的数组值的投影:

and I would like to obtain a projection of the array's values concatenated in a single field:

{
    "_id": "3_0",
    "values": "1_2"
}

这可能吗?我试过 $concat 但我想我不能使用 $values 作为 $concat 的数组.

Is this possible? I have tried $concat but I guess I can't use $values as the array for $concat.

推荐答案

在现代 MongoDB 版本中,您可以.您仍然不能直接"将数组应用于 $concat,但是您可以使用 $reduce 处理数组元素并产生这个:

In Modern MongoDB releases you can. You still cannot "directly" apply an array to $concat, however you can use $reduce to work with the array elements and produce this:

db.collection.aggregate([
  { "$addFields": {
    "values": { 
      "$reduce": {
        "input": "$values",
        "initialValue": "",
        "in": {
          "$cond": {
            "if": { "$eq": [ { "$indexOfArray": [ "$values", "$$this" ] }, 0 ] },
            "then": { "$concat": [ "$$value", "$$this" ] },
            "else": { "$concat": [ "$$value", "_", "$$this" ] }
          }    
        }
      }        
    }
  }}
])

当然结合 $indexOfArray 为了"_" 下划线连接",当它是数组的第一个"索引时.

Combining of course with $indexOfArray in order to not "concatenate" with the "_" underscore when it is the "first" index of the array.

我的额外愿望"也用 $sum 回答:

Also my additional "wish" has been answered with $sum:

db.collection.aggregate([
  { "$addFields": {
    "total": { "$sum": "$items.value" }
  }}
])

<小时>

这种类型的聚合运算符通常会增加一些元素数组.这里的区别在于,它表示编码表示中提供的参数"的数组",与当前文档中存在的数组元素"相对.


This kind of gets raised a bit in general with aggregation operators that take an array of items. The distinction here is that it means an "array" of "aguments" provided in the coded representation a opposed to an "array element" present in the current document.

您真正能够在文档中的数组中串联项目的唯一方法是执行某种 JavaScript 选项,就像 mapReduce:

The only way you can really do the kind of concatenation of items within an array present in the document is to do some kind of JavaScript option, as with this example in mapReduce:

db.collection.mapReduce(
    function() {
        emit( this._id, { "values": this.values.join("_") } );
    },
    function() {},
    { "out": { "inline": 1 } }
)

当然,如果您实际上没有聚合任何东西,那么最好的方法可能是在您的客户端代码中简单地执行连接"操作,以对查询结果进行后处理.但是如果它需要跨文档用于某些目的,那么 mapReduce 将是唯一可以使用它的地方.

Of course if you are not actually aggregating anything, then possibly the best approach is to simply do that "join" operation within your client code in post processing your query results. But if it needs to be used in some purpose across documents then mapReduce is going to be the only place you can use it.

我可以添加例如"我喜欢这样的工作:

I could add that "for example" I would love for something like this to work:

{
    "items": [
        { "product": "A", "value": 1 },
        { "product": "B", "value": 2 },
        { "product": "C", "value": 3 }
    ]
}

总和:

db.collection.aggregate([
    { "$project": {
        "total": { "$add": [
            { "$map": {
                "input": "$items",
                "as": "i",
                "in": "$$i.value"
            }}
        ]}
    }}
])

但它不能那样工作,因为 $add 需要参数,而不是文档中的数组.叹!:(.部分按设计"推理可以认为仅仅因为"它是从转换结果传入的奇异值的数组或列表",因此不能保证"这些是实际上是运算符期望的有效"单数数值类型值.至少不是在当前实现的类型检查"方法中.

But it does not work that way because $add expects arguments as opposed to an array from the document. Sigh! :(. Part of the "by design" reasoning for this could be argued that "just because" it is an array or "list" of singular values being passed in from the result of the transformation it is not "guaranteed" that those are actually "valid" singular numeric type values that the operator expects. At least not at the current implemented methods of "type checking".

这意味着现在我们仍然必须这样做:

That means for now we still have to do this:

db.collection.aggregate([
   { "$unwind": "$items" },
   { "$group": {
       "_id": "$_id",
        "total": { "$sum": "$items.value" }
   }}
])

而且遗憾的是,也无法应用这样的分组运算符来连接字符串.

And also sadly there would be no way to apply such a grouping operator to concatenate strings either.

因此您可以希望对此进行某种更改,或者希望进行某种更改以允许在 $map 以某种方式进行操作.更好的是,还欢迎新的 $join 操作.但这些在撰写时并不存在,而且可能在未来一段时间内不会存在.

So you can hope for some sort of change on this, or hope for some change that allows an externally scoped variable to be altered within the scope of a $map operation in some way. Better yet a new $join operation would be welcome as well. But these do not exist as of writing, and probably will not for some time to come.

这篇关于在 MongoDB 的单个字段中连接数组中的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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