Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.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
Node.js Mongo连接字符串数组_Node.js_Mongodb - Fatal编程技术网

Node.js Mongo连接字符串数组

Node.js Mongo连接字符串数组,node.js,mongodb,Node.js,Mongodb,我正在编写一个应用程序来使用Mongo数据库中的数据,但我需要以平面格式处理数据。我的文档中有几个数组,我想将它们转换为字符串数组,但找不到使用mongo查询的方法 文件: { name: 'Hello World', tags: ['simple', 'easy'] } 期望输出: { name: 'Hello World', tags: 'simple,easy', } 下面是我能想到的使用聚合查询实现这一点的唯一方法,但它是非常静态的 db.test.aggregate

我正在编写一个应用程序来使用Mongo数据库中的数据,但我需要以平面格式处理数据。我的文档中有几个数组,我想将它们转换为字符串数组,但找不到使用mongo查询的方法

文件:

{
  name: 'Hello World',
  tags: ['simple', 'easy']
}
期望输出:

{
  name: 'Hello World',
  tags: 'simple,easy',
}

下面是我能想到的使用聚合查询实现这一点的唯一方法,但它是非常静态的

db.test.aggregate([
    { $unwind: "$tags" },
    { $group: { _id: "$_id", name: {$first: "$name"}, firstTag: {$first: "$tags"}, lastTag: {$last: "$tags"} } },
    { $project: { name: "$name", tags: { $concat: [ "$firstTag", ",", "$lastTag" ] } } }
]);
但是,您可以使用mapReduce实现这一点:

db.test.mapReduce(
   function() {emit(this._id, this);},
   function(key, value) {return value}, {   
      out:"tags",
      finalize: function(key, reducedVal){
            reducedVal.tags = reducedVal.tags.join();
            return reducedVal;
      }
   }
)

> db.tags.find()
{ "_id" : ObjectId("5849a9f6a4db9c5811299d08"), "value" : { "name" : "Hello World", "tags" : "simple,easy" } }

下面是我能想到的使用聚合查询实现这一点的唯一方法,但它是非常静态的

db.test.aggregate([
    { $unwind: "$tags" },
    { $group: { _id: "$_id", name: {$first: "$name"}, firstTag: {$first: "$tags"}, lastTag: {$last: "$tags"} } },
    { $project: { name: "$name", tags: { $concat: [ "$firstTag", ",", "$lastTag" ] } } }
]);
但是,您可以使用mapReduce实现这一点:

db.test.mapReduce(
   function() {emit(this._id, this);},
   function(key, value) {return value}, {   
      out:"tags",
      finalize: function(key, reducedVal){
            reducedVal.tags = reducedVal.tags.join();
            return reducedVal;
      }
   }
)

> db.tags.find()
{ "_id" : ObjectId("5849a9f6a4db9c5811299d08"), "value" : { "name" : "Hello World", "tags" : "simple,easy" } }

可以使用
$reduce
在MongoDB聚合中以本机方式完成此操作:

db.myCollection.aggregate([{
  $set: {
     tags: {
       $reduce: {
         input: "$tags",
         initialValue: "",
         in: {
           $concat : ["$$value", "," ,"$$this"]
         }
       }
     }
  }, {
  $project: {
     name: 1,
     tags: { $substr: [ "$tags", 1, -1 ] }
  }
}]);
在使用
$concat
时,无法避免额外的分隔符(前导或尾随),因此需要使用
$substr
执行额外的步骤来删除它

  • $concat:[“$$value”,“,”,“$$this”]
    生成字符串
    ”,a,b,c,d“
  • $substr:[“$tags”,1,-1]
    使子字符串从索引
    1
    开始,长度
    -1
    ,在本上下文中是“尽可能多的”

  • 另请参阅文档。

    可以使用
    $reduce
    在MongoDB聚合中本机完成:

    db.myCollection.aggregate([{
      $set: {
         tags: {
           $reduce: {
             input: "$tags",
             initialValue: "",
             in: {
               $concat : ["$$value", "," ,"$$this"]
             }
           }
         }
      }, {
      $project: {
         name: 1,
         tags: { $substr: [ "$tags", 1, -1 ] }
      }
    }]);
    
    在使用
    $concat
    时,无法避免额外的分隔符(前导或尾随),因此需要使用
    $substr
    执行额外的步骤来删除它

  • $concat:[“$$value”,“,”,“$$this”]
    生成字符串
    ”,a,b,c,d“
  • $substr:[“$tags”,1,-1]
    使子字符串从索引
    1
    开始,长度
    -1
    ,在本上下文中是“尽可能多的”

  • 另请参阅文档。

    我认为您需要使用
    mapReduce
    来实现这一点。如果可能的话,最好在客户端进行。为什么不在检索文档时进行呢?@wiredparie说。只需使用
    toString()
    。我想您需要使用
    mapReduce
    来实现这一点。如果可能的话,最好在客户端进行。为什么不在检索文档时进行呢?@wiredparie说。只需使用
    toString()