蒙古不同的数量和地点

我需要在Mongo中运行Distinct,Count和Where查询的帮助。 集合被称为“trackedevents”

{ Type: 'Asset Search', User: 'ABC' Timestamp: '26/01/2015' }, { Type: 'Asset Search', User: 'DEF' Timestamp : '27/01/2015' } 

我需要帮助的是让mongo给我一个特定的时间戳范围内的types“资产search”

我可以比较容易地在SQL中做到这一点,但mongo只是感觉更加复杂。 这里的一些专家可以帮助我吗?

你可以通过几种方法来解决这个问题。 首先是使用汇总框架,它使用$match$grouppipe道步骤来查询和分组文档,然后通过用户字段进行计数:

 var pipeline = [ { "$match": { "Type": "Asset Search", "Timestamp": { "$gte": start, "$lte": end } } }, { "$group": { "_id": "$User", "count": { "$sum": 1 } } } ]; TrackedEvent.aggregate(pipeline, function(err, result){ if (err) { /* Handle error */ } console.log(result); }) 

另一种方法是使用distinct()count()方法的组合

 var query = { "Type": "Asset Search", "Timestamp": { "$gte": start, "$lte": end } }; TrackedEvent.distinct('User', query).count().exec(function (err, count) { console.log('The number of unique users is: %d', count); }); 
 User.find() .and([{'Type': 'Asset Search'}, $and: [ {'Timestamp': {$gt: date1}}, {'Timestamp': {$lt: date2}} ] .count() .exec(function (err, userCount) { //do stuff }) 

如果你想在两个date之间的范围,你可能不得不添加一个和条件在和。 目前这个查询哟将获得更多的req.params.date时间戳所有用户的计数,并键入“资产search”。 也是你的时间戳字段真的是一个时间戳数组?

/ /现在编辑它像你想的那样