Tag: aggregation framework

select当月有生日的所有用户

我刚开始学习NodeJs + MongoDB(Mongoose)。 查询有问题。 我需要select当月有生日的所有用户 。 用户架构: const UserSchema = new mongoose.Schema({ email: { type: String, unique: true, required: true, index: true }, password: { type: String, required: true }, firstName: { type: String, required: true }, lastName: { type: String, required: true }, phone: { type: String, required: true }, birthday: { type: Date, […]

Nodejs + Mongodb:聚合后查找数据

我是Nodejs和MongoDB的新手。 以下是我的数据集示例: { 'name': ABC, 'age':24, 'gender':male, … } 一般来说,我想要做的就是在使用它们来查找不同的数据集群之前对数据进行聚合。 具体来说,我想知道有多less人在不同的年龄段。 然后,find每个年龄段的人(文件)并存储它们。 这是我的代码: MongoClient.connect(url, function(err, db) { if(err) { console.log('Unable to connect to the mongoDB server. Error:', err); } else { db.collection('test').aggregate( [ { $group: { _id: "$age" , total: { $sum: 1 } } }, { $sort: { total: -1 } } ]).toArray(function(err, result) […]

Mongoose在聚合中使用子查询来总结数据

我是mongoose新手。 我有这样的模型。 var ForumSchema = new mongoose.Schema({ User_id : {type : Schema.Types.ObjectId, ref : 'User'}, Title : {type : String}, Content : {type : String}, Tags : [{type : String}], isPublic : {type : Boolean, default : false}, Vote : [{ User_id : {type : Schema.Types.ObjectId, ref : 'User'}, Kind : {type : Boolean} }], […]

按类别和date按date分组数据

我需要执行以下操作: 按date进行分组,然后logging总数和logging总数 按类别分组,然后聚合必须由所有类别的upvote总数和每天upvote和所有date的倒数投票所有类别的downvote。 这是我的代码: db.collection.aggregate([{ $unwind: '$votes' }, { $match: { 'category_id': array[i] } }, { $group: { _id:'$votes.date', "category_id": { $first: "$category_id" }, up_vote: { $sum: { $cond: [{ '$gt': ['$votes.score', 0] }, "$votes.score", 0] } }, down_vote: { $sum: { $cond: [{ '$lt': ['$votes.score', 0] }, "$votes.score", 0] } } } }, { […]

返回date范围内的数组文档

是否有可能连锁finById() , select()和find()在mongoose? 我正在尝试这样的事情,它不接受date,而只是返回在familyStats集合中的所有对象。 module.exports.getFamilyStatsPast = function (req, res) { var id = req.params.id; var days = req.params.days; var pastDate = new Date(); pastDate.setDate(pastDate.getDate() – days).toISOString(); var currentDate = new Date().toISOString(); Worksets .findById(id) .select('familyStats') .find({ createdOn : { $gte: pastDate, $lte: currentDate } }) .exec(function (err, doc){ var response = { status: 200, message: [] […]

如何使用$ split运算符进行聚合

我想在date字段上使用$ split运算符。 [{ "status": true, "beaconData" :{ "date" : "15/07/2017, 11:00", "charge" : 15, }, { "status": true, "beaconData" :{ "date" : "2/07/2017, 15:00", "charge" : 35, }, { "status": true, "beaconData" :{ "date" : "2/07/2017, 11:05", "charge" : 5, }] 我曾经这样用过 db.orders.aggregate([{$ project:{data:{$ split:[“$ beaconData.date”,“,”]},'status':1}}]) 但错误返回像 “errmsg”:“exception:无效的操作符'$ split'”, “代码”:15999,

MongoDB平均embedded式数组作为额外的领域

我的文件看起来像这样: { "_id" : "53ce85eda2579da8b40c1f0f", "name" : "Autokino", "tags" : [ "forMen" ], "ratings" : [ { "rating" : 5, "uuid" : "…"}, { "rating" : 4, "uuid" : "…"}, { "rating" : 4, "uuid" : "…"}, { "rating" : 1, "uuid" : "…"}, ] } 现在我需要ratings.rating的平均值(这里应该是3.5)。 我的查询如下所示: activities.aggregate([ { $match: { _id: ObjectID(req.params.id) } […]

MongoDB将相关的收集项目数与其他收集结果合并

我是新来的MongoDB,并试图找出如何有效地查询集合中的每个项目。 我有projects集合和tasks集合 //projects { _id: ObjectId(), name: String } //tasks { _id: ObjectId(), projectId: ObjectId(), //reference project id completed: Bool } 我想获得所有的项目,然后计算每个项目已completed和incomplete任务 db.projects.find({})… //perhaps something similar in output [ { _id: ObjectId(), //projectId name: String completed: Number, incomplete: Number } ] 我使用mongoose作为ORM。 我不知道这是可能的mongoose,甚至本地mongodb查询。 感谢任何帮助。 谢谢!

Mongoose:嵌套数组结构中的聚合查询

我遇到了一个MongoDB查询的问题。 我有一个名为“Answers”的集合,具有以下结构 { "_id": ObjectId("560acfcb904a29446a6d2617"), "path_id": ObjectId("560acae1904a29446a6d2610"), "step_id": "kids", "user_id": ObjectId("559ae27684ff12e88d194eb7"), "answers": [ { "id": "kids_q", "question": "Do you have kids?", "answer": "No" } ] } 正如你所看到的answers是一个数组,它可以有一个或多个对象,但总是一个数组。 首先,我想在step_id中得到答案的总数 我得到了使用聚合使用下面的查询 Answer.aggregate( { $match: { 'path_id': { $eq: path_id }, 'step_id': { $eq: step_id }, '' } }, { $group: { _id: { step_id: '$step_id' }, count: […]

mongoose – 用$ sum使用聚合创build新的属性

我试图在包含数字数组之和的所有文档中添加一个新字段。 这里是架构(为简洁起见删除无关字段): var PollSchema = new Schema( { votes: [Number] } ); 我build立模型: PollModel = mongoose.model('Poll', PollSchema); 我使用聚合创build一个新的字段,其中包含投票数组的总和。 PollModel.aggregate([ { $project: { totalVotes: { $sum: "$votes"} } } ]); 当我启动我的服务器,我没有得到任何错误; 但是, totalVotes字段尚未创build。 我用这个文档来帮助我。 它同样使用$sum运算符,我完全像文档说明的那样,但没有结果。