我想查询一下我的数据库MongoDB中有更多tweets的用户排名:
var TeewtSchema = new Schema({
userId: Number,
twweetId : Number,
createdAt: Date,
cuerpo: String,
nameUser: String,
location: String,
avatar: String,
user: String
});
mysql输出类似于:
SELECT *, (SELECT COUNT(*) FROM `TABLE 1` WHERE userId = t.userId ) rank FROM `TABLE 1` t GROUP BY t.userId ORDER BY rank DESC
但是在MongoDB中我不知道该怎么做
最佳答案
是的,正如johnnyhk建议的那样,您应该使用aggregation framework来实现这一点。应该采取如下措施:
db.collection.aggregate(
{ $group : { _id : "$userId", numTweets = { $sum : 1 } }, // sum tweets over each user
{ $sort : { numTweets : -1 } } // sort by numTweets in descending order
)
关于mysql - 在select - mongoDB中计算子查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18425236/