How to perform $out in an aggregation in mongoose?

坚强是说给别人听的谎言 提交于 2021-02-19 08:53:12

问题


I looked for documentation of how to perform $out in an aggregation but I didn't find.

This is my query:

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) });

Schema:

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

It might to be compatible with mongodb 3.0.x


回答1:


I checked the database and it seems to create a new collection something like 'aggr_out' with the entries. I'm considering it as solved.




回答2:


Most of queries might be done by using node-mongodb-native on Mongoose. For an out option the result would be the same.

Also it will create a collection for you automatically.

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!") 
    }
);


来源:https://stackoverflow.com/questions/34333284/how-to-perform-out-in-an-aggregation-in-mongoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!