Skip to content

Instantly share code, notes, and snippets.

@alexanderblackh
Created April 6, 2017 22:47
Show Gist options
  • Save alexanderblackh/d6010a6eeedaba0553bfb9e8561b8601 to your computer and use it in GitHub Desktop.
Save alexanderblackh/d6010a6eeedaba0553bfb9e8561b8601 to your computer and use it in GitHub Desktop.
db.entries
.aggregate([
{$match: {'context.plugin': 'twitter', 'context.action': 'getTimeline', 'data.text._processed.language_tone.confidence': {$not: {$type: 10}}}},
{$addFields: {twitter_username: { "$ifNull": [ "$data.user.screen_name._raw", "$data.screen_name._raw" ] } }},
{$group: {_id: '$twitter_username',language_tones: {$push: '$data.text._processed.language_tone' }}},
{$unwind: '$language_tones'},
{$unwind: '$language_tones'},
{$group: {_id: '$_id', scores: {$push: '$language_tones'}}},
{$project: {_id: 1,
//The double $$ is what's causing the issues
analytical: {$filter: {input: '$scores', as: 'score', cond: {$eq: ['$$score.value', 'analytical']}}},
confidence: {$filter: {input: '$scores', as: 'score', cond: {$eq: ['$$score.value', 'confidence']}}},
tentative: {$filter: {input: '$scores', as: 'score', cond: {$eq: ['$$score.value', 'tentative']}}},
}},
{ "$addFields": {"analytical_average": {"$divide": [{"$reduce": {"input": "$analytical","initialValue": 0,"in": { "$add": ["$$value", "$$this.confidence"] }}},{"$cond": [{ "$ne": [ { "$size": "$analytical" }, 0 ] },{ "$size": "$analytical" }, 1]}]}},},
{ "$addFields": { "confidence_average": {"$divide": [{"$reduce": {"input": "$confidence","initialValue": 0,"in": { "$add": ["$$value", "$$this.confidence"] }}},{"$cond": [{ "$ne": [ { "$size": "$confidence" }, 0 ] },{ "$size": "$confidence" }, 1]}]}},},
{ "$addFields": {"tentative_average": {"$divide": [{"$reduce": {"input": "$tentative","initialValue": 0,"in": { "$add": ["$$value", "$$this.confidence"] }}},{"$cond": [{ "$ne": [ { "$size": "$tentative" }, 0 ] },{ "$size": "$tentative" }, 1]}]},}},
{$sort: {retweet_sum: -1, favorite_sum: -1}}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment