2017-09-06 5 views
-4

例えばMongoDBの集計方法

どうdb.anycollection.aggregateから結果を表示するために(..)

// drop collectio 
db.collections.drop() 
// insert data 
db.collections.insertMany([ 
{cust_id: "A123", anount: 500, status : "A"}, 
{cust_id: "A123", anount: 250, status : "A"}, 
{cust_id: "B212", anount: 200, status : "A"}, 
{cust_id: "A123", anount: 300, status : "D"} 
]) 
// aggregate throuhgh thr collection 
var results = db.collection.aggregate([ 
    {$match:{status:"A"}}, 
    {$group:{_id: "$cust_id", total:{$sum:"$amount"))) 


    ]) 

答えて

0

あなたはこれを行うことができます - これにより

var Member = mongoose.model("Members", memberSchema); 

Member.aggregate(
{ "$match": { "_id": userid } }, 
{ "$unwind": "$friends" }, 
{ "$match": { "friends.status": 0 } }, 
function(err, data) { 

if (err) 
    throw err; 

console.log(JSON.stringify(data, undefined, 2)); 

} 

を集計結果を表示することができます。