2016-11-21 4 views
0

ich Abfrage etwas wie dieses:

{"$match": {$or: [{"to": system.mongoose.Types.ObjectId(userId)}, {"from": system.mongoose.Types.ObjectId(userId)}]}}, 
     {"$sort": {"createDate": -1}}, 
     { 
      "$group": { 
       "_id": "$conversationId", 
       "from": {"$first": "$from"}, 
       "to": {"$first": "$to"}, 
       "content": {"$first": "$content"}, 
       "createDate": {"$first": "$createDate"}, 
       "unreaded": {"$sum": 1} 
      } 

     }, 

Ich brauche keine Haupt-$match zu bearbeiten Ich brauche nur Summe von unreaded Nachrichten zu erhalten,

ich so etwas wie dieses brauchen : "unreaded": {"$sum": 1, {$match: {unreaded: true}}}

Irgendeine Lösung für dieses?

Antwort

0

Verwenden $cond

"unreaded": {"$sum": {$cond: {if: "$unreaded", then: 1, else: 0} }} 

Dokumentation:
https://docs.mongodb.com/manual/reference/operator/aggregation/cond/

+0

Wie kann ich auch eine andere cond es? Ich muss auch "$ to" gleich sein "system.mongoose.Types.ObjectId (userId)" Benötigen Sie etwas wie folgt: '" nicht definiert ": {" $ sum ": {$ cond: {if:" $ unloaded " && ("$ to" == system.mongoose.Types.ObjectId (userId)), dann: 1, sonst: 0}}}? – Vladimir

Verwandte Themen