2017-09-05 5 views
0

ich ein kleines Problem Whit $ Gruppe haben, ich auf alle Beiträge Tags zählen müssen (auf tags.post Feld zählen bekommen) von Post SammlungMongoDB Aggregation

Ich benutze moongose ​​ODM

Beitrag Beispielmodell :

var PostSchema = new Schema({ 
     inc: { 
      type: Number 
     }, 
     title: { 
      type: String, 
      required: true 
     }, 
     description: { 
      type: String, 
      required: true 
     }, 
     tags:{ 
      post:[{ type: Schema.Types.ObjectId, ref: 'Tag'}], 
      system:[{ type: Schema.Types.ObjectId, ref: 'Tag' }] 
     } 

    }); 
    var Post = Mongoose.model('Post', PostSchema, 'posts'); 

    module.exports = Post; 

Tag Beispielmodell:

var TagSchema = new Schema({ 

    name: { 
     index: { unique: true }, 
     type: String, 
     required: true 
    } 
}); 

var Tag = Mongoose.model('Tag', TagSchema, 'tags'); 

module.exports = Tag; 

Das Ergebnis sollte wie folgt sein:

[ 
{ 
"tags_id":"12345667", 
"count": 4 
}, 
{ 
"tags_id":"12345668", 
"count": 3 
}, 
{ 
"tags_id":"12345669", 
"count": 2 
}, 
{ 
"tags_id":"12345660", 
"count": 1 
} 
] 

In SQL-Datenbank wie es aussah ist haben

SELECT tag_id, COUNT(*) AS count 
FROM posts 
GROUP BY tag_id; 

ich keine Erfahrung Whit MongoDB

Das ist mein versucht

Post.aggregate([ 
{ 
$match: { 
    "tags.post": { 
    $ne: [] 
    } 
}, 
{ 
$lookup:{ 
    from: "tags", 
    localField: "tags.post", 
    foreignField: "_id", 
    as: "tags" 
} 
}, 
{ 
$group: { 
    _id: '$tags._id', 
    count: {'$sum':1} 
} 
}], function (err, result) { 
if (err) { 
    return console.log(err); 
} 
    return console.log(result); 
}); 

Ergebnis:

[ 
    { 
     "_id": [ 
      "59ad4cfe454aaf4f46f5dcea", 
      "59ad4ff994190a4b8acc6871", 
      "59ad4ff994190a4b8acc6872", 
      "59ad65bd454aaf4f46f5dd15" 
     ], 
     "count": 1 
    }, 
    { 
     "_id": [ 
      "59ad65bd454aaf4f46f5dd15" 
     ], 
     "count": 1 
    }, 
    { 
     "_id": [ 
      "59ae20e19d094c31d3751781" 
     ], 
     "count": 1 
    }, 
    { 
     "_id": [ 
      "59ad4cfe454aaf4f46f5dcea" 
     ], 
     "count": 1 
    }, 
    { 
     "_id": [ 
      "59ad4fcb454aaf4f46f5dd02" 
     ], 
     "count": 1 
    } 
] 

Dank

+0

Haben Sie etwas vor um Hilfe zu bitten versucht? –

+0

Ich habe deine Frage nicht verstanden ... natürlich habe ich es versucht !!! aber $ group return list und duplicate tags –

+0

Warum zeigen Sie uns dann nicht, was Sie dann versucht haben, damit wir es korrigieren können? Höchstwahrscheinlich war Ihre "Reduzieren" -Funktion falsch. –

Antwort

1

Nur noch ein Feld wie unten in $group verwenden,

"Count": { "$sum": 1}

Dies wird Ihnen zählen.

0

gefunden Lösung:

Tag.aggregate([ 
      { 
       $lookup: { 
        from: "posts", 
        localField: "_id", 
        foreignField: "tags.post", 
        as: "posts" 
       } 
      }, 
      { 
       $unwind: "$posts" 
      }, 
      { 
       $group: {_id:"$_id", posts: {$push:"$posts"},name : { $first: '$name' }, size: {$sum:1}} 
      }, 
      { 
       $project : { name : 1, size:1 } 
      }, 
      { 
       $sort:{size:-1} 
      }, 
      { 
       $limit : limit 
      } 
     ], function (err, result) { 
      if (err) { 
       return utils.res.error(res, { message: 'Could not fetch all public tags', reason: err, debug: result }) 
      } 
      return utils.res.ok(res, result); 
     }) 

Dank an alle, wenn jemand Lösung Teig hat, schreiben Sie bitte hier werde ich dankbar