Fernanda Fentili
Fernanda Fentili

Reputation: 177

How to perform $out in an aggregation in mongoose?

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

Upvotes: 1

Views: 1900

Answers (2)

efkan
efkan

Reputation: 13217

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

Upvotes: 1

Fernanda Fentili
Fernanda Fentili

Reputation: 177

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.

Upvotes: 2

Related Questions