Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MongoDB查询以查找集合的所有数组元素_Php_Mongodb_Mongodb Php_Nosql - Fatal编程技术网

Php MongoDB查询以查找集合的所有数组元素

Php MongoDB查询以查找集合的所有数组元素,php,mongodb,mongodb-php,nosql,Php,Mongodb,Mongodb Php,Nosql,我有一个相当大的MongoDB文档,其中包含各种数据。我需要识别集合中类型为array的字段,以便从将填充的网格中显示的字段中删除它们 我的方法现在包括使用 这是从这里发布的回复中获取的 并为每个字段运行如下查询 db.Product.find( { $where : "Array.isArray(this.Orders)" } ).count() 如果检索到任何内容,则该字段被视为数组 我不喜欢这样,我需要运行n+2个查询(n是集合中不同字段的数量),并且我不想硬编码模型中的字段。它将破坏使

我有一个相当大的MongoDB文档,其中包含各种数据。我需要识别集合中类型为array的字段,以便从将填充的网格中显示的字段中删除它们

我的方法现在包括使用

这是从这里发布的回复中获取的

并为每个字段运行如下查询

db.Product.find( { $where : "Array.isArray(this.Orders)" } ).count()
如果检索到任何内容,则该字段被视为数组

我不喜欢这样,我需要运行n+2个查询(n是集合中不同字段的数量),并且我不想硬编码模型中的字段。它将破坏使用MongoDB的全部目的


有更好的方法吗?

我对上面提供的代码做了一些轻微的修改:

mr = db.runCommand({
  "mapreduce" : "Product",
  "map" : function() {
    for (var key in this) { 
       if (Array.isArray(this[key])) {
          emit(key, 1); 
       } else {
          emit(key, 0);
       }
    }
  },
  "reduce" : function(key, stuff) { return Array.sum(stuff); }, 
  "out": "Product" + "_keys"
})
现在,映射器将为包含数组的键发出1,为不包含数组的键发出0。减速器将汇总这些结果,以便在检查最终结果时:

db[mr.result].find()
您将看到您的字段名及其包含数组值的文档数(对于任何不属于数组的字段,都为0)

因此,这将为您提供哪些字段只包含map reduce作业的数组类型

--

只是想用一些数据来看一下:

db.Product.insert({"a":[1,2,3], "c":[1,2]})
db.Product.insert({"a":1, "b":2})
db.Product.insert({"a":1, "c":[2,3]})
(现在运行上面的“mr=”代码)

db.Product.insert({"a":[1,2,3], "c":[1,2]})
db.Product.insert({"a":1, "b":2})
db.Product.insert({"a":1, "c":[2,3]})
db[mr.result].find()
{ "_id" : "_id", "value" : 0 }
{ "_id" : "a", "value" : 1 }
{ "_id" : "b", "value" : 0 }
{ "_id" : "c", "value" : 2 }