arrays – MongoDB – 查询对数组参数元素的操作

我是MongoDB的新手,我正试图了解我是否可以使用MongoDB方便地执行此查询并获得不错的性能.我想将数值和数组参数传递给查询,并使用它们对集合中每个文档中的数组值执行逐元素操作.这可能吗?

例如集合包含以下文档:

{
    "name" : "item1",
    "m" : 5.2,
    "v" : 1.1,
    "data1" : [ 0, 0, 0.3, 0.7, 0.95, 0.9, 0.75, 0.4, 0.1, 0 ],
    "data2" : [ 0, -1, 0, 1, 1, 0 ]
}

我有另一个“搜索”文档可能看起来像这样:

{
    "x" : 8,
    "K" : 1,
    "dataA" : [ 0, 0, 0, 0, 0, 0, 0, 0.5, 1, 0.5],
    "dataB" : [ 0, -2, 0, 1, 1, 0 ]
}

我想运行一个查询或map-reduce,使用上面的搜索文档对上面的集合返回一个包含以下内容的集合:

{
    "name",
    "y" = fn(m, v, x, K) = Kvx^(1/m) (not the real formula but just an example)
    "dataF" = Max(i=0..9) {data1[i] * dataA[i] }
    "dataS" = Sum(j=0..5) {data2[j] * dataB[j] }
}
where y>0

因此,对于上面的示例,返回的结果将是

{
    "name" : "item1",
    "y" : 1 * 1.1 * 8^5.2 = 1.641
    "dataF" : Max(..., 0.4*0.5, 0.1*1, 0 * 0.5 ) = 0.2
    "dataS" : 0*0 + (-1)*(-2) + 0*0 + 1*1 + 1*1 + 0*0 = 4
}

使用MongoDB可以/方便吗?

注意:在我的应用程序中,使用标准的MongoDB操作将在搜索中包含更多标准条件,因此我希望在查询中包含上述处理并避免在客户端上执行此操作.

最佳答案 这是map / reduce版本:

db.data.save({
  "name" : "item1",
  "m" : 5.2,
  "v" : 1.1,
  "data1" : [ 0, 0, 0.3, 0.7, 0.95, 0.9, 0.75, 0.4, 0.1, 0 ],
  "data2" : [ 0, -1, 0, 1, 1, 0 ]
});

db.data.mapReduce( function() {
  var searchdoc = {
    "x" : 8,
    "K" : 1,
    "dataA" : [ 0, 0, 0, 0, 0, 0, 0, 0.5, 1, 0.5],
    "dataB" : [ 0, -2, 0, 1, 1, 0 ]
  };

  var result = {name: this.name};
  result.y = searchdoc.K * this.v * Math.pow(searchdoc.x, 1 / this.m);
  if(result.y > 0) {
    result.dataF = 0;
    for(i=0;i<this.data1.length;i++) {
      var f = this.data1[i] * searchdoc.dataA[i];
      if(f > result.dataF) {
        result.dataF = f;
      }
    } 
    result.dataS = 0;
    for(i=0;i<this.data2.length;i++) {
      var s = this.data2[i] * searchdoc.dataB[i];
      result.dataS += s;
    } 
    emit(this.name, result);
  }
}, function(key, values){}, {out: {inline: 1}});

结果:

{
"results" : [
    {
        "_id" : "item1",
        "value" : {
            "name" : "item1",
            "y" : 1.640830939540542,
            "dataF" : 0.2,
            "dataS" : 4
        }
    }
],
"timeMillis" : 0,
"counts" : {
    "input" : 1,
    "emit" : 1,
    "reduce" : 0,
    "output" : 1
},
"ok" : 1,
}

这是shell版本:

db.data.save({
  "name" : "item1",
  "m" : 5.2,
  "v" : 1.1,
  "data1" : [ 0, 0, 0.3, 0.7, 0.95, 0.9, 0.75, 0.4, 0.1, 0 ],
  "data2" : [ 0, -1, 0, 1, 1, 0 ]
});

var searchdoc = {
  "x" : 8,
  "K" : 1,
  "dataA" : [ 0, 0, 0, 0, 0, 0, 0, 0.5, 1, 0.5],
  "dataB" : [ 0, -2, 0, 1, 1, 0 ]
};

var search = function(searchdoc) {
  db.data.find().forEach(function(obj) {
    var result = {name:obj.name};
    result.y = searchdoc.K * obj.v * Math.pow(searchdoc.x, 1 / obj.m);
    if( result.y > 0 ) {
      result.dataF = 0;
      for(i=0;i<obj.data1.length;i++) {
        var f = obj.data1[i] * searchdoc.dataA[i];
        if(f > result.dataF) {
          result.dataF = f;
        }
      } 
      result.dataS = 0;
      for(i=0;i<obj.data2.length;i++) {
        var s = obj.data2[i] * searchdoc.dataB[i];
        result.dataS += s;
      } 
      db.results.save(result); 
    }
  });
}

search(searchdoc);

db.results.find();
{ "_id" : ObjectId("4f08ffe4264d23670eeaaadf"), "name" : "item1", "y" : 1.640830939540542, "dataF" : 0.2, "dataS" : 4 }
点赞