如何在mongoose中的聚合中执行$out?

如何在mongoose中的聚合中执行$out?,mongoose,aggregation,out,Mongoose,Aggregation,Out,我查找了如何在聚合中执行$out的文档,但没有找到 这是我的疑问: Top.aggregate([ {$sort: {created: -1}}, {$group: {_id:'$location', title:{$push: '$title'}}}, {$project: {location: '$location', mostRecentTitle: '$title'}}, {$out: "aggr_out"} ]).exec(function(err, docs) {

我查找了如何在聚合中执行$out的文档,但没有找到

这是我的疑问:

Top.aggregate([
  {$sort: {created: -1}},
  {$group: {_id:'$location', title:{$push: '$title'}}},
  {$project: {location: '$location', mostRecentTitle: '$title'}},
  {$out: "aggr_out"}
]).exec(function(err, docs) {  console.log(docs); console.log(err) });
模式:

var schema = mongoose.Schema({
  location: {type: String},
  title: {type: String},
  created: {type: Number, default: Math.floor(new Date() / 1000)}
})

它可能与mongodb 3.0.x兼容

我检查了数据库,它似乎创建了一个新的集合,类似于“aggr_out”的条目。我认为它已经解决了。

大多数查询可能通过使用
Mongoose
上的
node mongodb native
完成。 对于
out
选项,结果将是相同的

它还会自动为您创建一个集合

Top.collection.aggregate([
      {$sort: {created: -1}},
      {$group: {_id:'$location', title:{$push: '$title'}}},
      {$project: {location: '$location', mostRecentTitle: '$title'}},
      {$out: "aggr_out"}
    ],
    function(err) {  
        if (err) console.log("error: ", err) 
        else console.log("the all documents have been written onto aggr_out!") 
    }
);

当我试图将Mongoose聚合保存到MongoDB集合中时,这节省了我的时间。在Mongoose AggregationCursor似乎不起作用的情况下,这是一种受欢迎的方法。聚合([{$sort:{created:-1}},{$group:{{u id:'$location',title:{$push:'$title'}}}},{$project:{location:'$location',mostRecentTitle:'$title'},{$out:'aggr_out'}]))