问题
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