【问题标题】:MongoDB aggregate methodMongoDB聚合方法
【发布时间】:2018-02-15 04:18:17
【问题描述】:

例如

如何显示来自 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")))


  ])

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    你可以这样做:-

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

    通过这样做,您可以显示汇总结果。

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 2012-05-10
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多