简体   繁体   English

将文档的 MongoDB 字段合并为一个文档

[英]Merging MongoDB fields of documents into one document

I'm using MongoDB 2.6.6我正在使用 MongoDB 2.6.6

I have these documents in a MongoDB collection and here is an example:我在 MongoDB 集合中有这些文档,这是一个示例:

{ ..., "field3" : { "one" : [ ISODate("2014-03-18T05:47:33Z"),ISODate("2014-06-02T20:00:25Z") ] }, ...}
{ ..., "field3" : { "two" : [ ISODate("2014-03-18T05:47:33Z"),ISODate("2014-06-02T20:00:25Z") ] }, ...}
{ ..., "field3" : { "three" : [ ISODate("2014-03-18T05:47:39Z"),ISODate("2014-03-19T20:18:38Z") ] }, ... }

I would like the merge these documents in one field.我想将这些文件合并在一个字段中。 For an example, I would like the new result to be as follows:例如,我希望新结果如下:

{ "field3", : { "all" : [ ISODate("2014-03-18T05:47:39Z"),ISODate("2014-03-19T20:18:38Z"),......  ] },}

I'm just not sure any more how to have that result!我只是不确定如何得到这个结果!

Doesn't really leave much to go on here but you can arguably get the kind of merged result with mapReduce:在这里并没有留下太多内容,但可以说您可以使用 mapReduce 获得那种合并结果:

db.collection.mapReduce(
  function() {
    var field = this.field3;

    Object.keys(field).forEach(function(key) {
      field[key].forEach(function(date) {
        emit( "field3", { "all": [date] } )
      });
    });
  },
  function (key,values) {

    var result  = { "all": [] };

    values.forEach(function(value) {
      value.all.forEach(function(date) {
        result.all.push( date );
      });
    });

    result.all.sort(function(a,b) { return a.valueOf()-b.valueOf() });

    return result;

  },
  { "out": { "inline": 1 } }
)

Which being mapReduce is not exactly in the same output format given it's own restrictions for doing things:考虑到它对做事的限制,mapReduce 的输出格式并不完全相同:

{
    "results" : [
            {
                    "_id" : "field3",
                    "value" : {
                            "all" : [
                                    ISODate("2014-03-18T05:47:33Z"),
                                    ISODate("2014-03-18T05:47:33Z"),
                                    ISODate("2014-03-18T05:47:39Z"),
                                    ISODate("2014-03-19T20:18:38Z"),
                                    ISODate("2014-06-02T20:00:25Z"),
                                    ISODate("2014-06-02T20:00:25Z")
                            ]
                    }
            }
    ],
    "timeMillis" : 86,
    "counts" : {
            "input" : 3,
            "emit" : 6,
            "reduce" : 1,
            "output" : 1
    },
    "ok" : 1
}

Since the aggregation here into a single document is fairly arbitrary you could pretty much argue that you simply take the same kind of approach in client code.由于此处聚合到单个文档中是相当随意的,您几乎可以争辩说您只是在客户端代码中采用相同类型的方法。

At any rate this is only going to be useful over a relatively small set of data with next to the same sort of restrictions on the client processing.无论如何,这只会对相对较小的数据集有用,并且对客户端处理具有相同的限制。 More than the 16MB BSON limit for MongoDB, but certainly limited by memory to be consumed.超过了 MongoDB 的 16MB BSON 限制,但肯定会受到要消耗的内存的限制。

So I presume you would need to add a "query" argument but it's not really clear from your question.所以我认为你需要添加一个“查询”参数,但你的问题并不是很清楚。 Either using mapReduce or your client code, you are basically going to need to follow this sort of process to "mash" the arrays together.无论是使用 mapReduce 还是您的客户端代码,您基本上都需要按照这种过程将数组“混搭”在一起。

I would personally go with the client code here.我个人会在这里使用客户端代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM