Tag: 聚合框架

迭代MongoDB聚合查询产生的数组

大家下午好, 我在MongoDB 3.4中使用聚合查询非常困难。 我有一个问题是要求我将聚合查询的结果推送到一个名为categories的空数组中,我已经能够成功地使用这个代码: var categories = []; database.collection("item").aggregate([{ $group : { _id : "$category", num : {$sum : 1} }}, {$sort:{_id:1}}]).toArray(function(err, data){ categories.push(…data); callback(categories); console.log(categories); }) } categories看起来像这样: [ { _id: 'Apparel', num: 6 }, { _id: 'Books', num: 3 }, { _id: 'Electronics', num: 3 }, { _id: 'Kitchen', num: 3 }, { […]

在node.js-mongodb中分组

我想在我的server.js中使用group,但是我得到这个错误: TypeError:Object #(Collection) has no method 'group' 我在网上看,看到这个函数的名字。 这是我写的: var monk = require('monk'); var db = monk('localhost:27017/Messages'); var collection = db.get('col'); collection.group(['a'], {}, {}, {}, function (e, docs) { res.json(docs); }); 谁能告诉我什么是错的? 谢谢!

与geonear的mongoose聚合

我试图用Mongoose geoNear命令实现分页。 它似乎跳过不支持与geoNear,我明白聚合将工作(与分页与性能成本)。 我怎样才能转换为聚合查询跳过一些文件? exports.near = function(req, res) { if(req.query.lat && req.query.lng) { var point = { type: "Point", coordinates : [Number(req.query.lng), Number(req.query.lat)] }; var queryParams = { spherical: true, skip: 0, limit: 10, distanceMultiplier: 6371 // radians to kilometers. (The radius of the Earth is approximately 3,959 miles or 6,371 kilometers.) }; if(req.query.q) { […]

Node.JS + MongoDB聚合框架的平均值

我一直在考虑使用Node和MongoDb作为Android应用程序的评分系统,到目前为止,我已经实现了创build,更新和删除罚款,但我不知道该怎么做,当我想从平均得分D B。 这里是我所决定的数据结构: [ { "_id": "1", "ratings": [ { "value": "5", "user": "CoolUser1" }, { "value": "4", "user": "CoolUser2" } ] }, { "_id": "2", "ratings": [ { "value": "4", "user": "CoolUser3" }, { "value": "2", "user": "CoolUser4" } ] } ] 基本上我需要的是一个给定_id的评分的平均值。 下面是我使用的Update方法,以了解如何设置mongoDB连接: exports.updateRating = function(req, res) { var id = req.params.id; var […]

mongodb聚合filter并计算nodejs

我有这个集合命名为“点” [ { 'guid': 'aaa' }, { 'guid': 'bbb' }, { 'guid': 'ccc' }, ] 我有另一个名为“stream” [ { 'title': 'pippo', 'guid': 'aaa', 'email': 'pippo@disney'}, { 'title': 'pluto', 'guid': 'aaa', 'email': 'pluto@disney'}, { 'title': 'goofy', 'guid': 'bbb', 'email': 'goofy@disney'}, { 'title': 'duck', 'guid': 'aaa', 'email': 'duck@disney'}, { 'title': 'minnie', 'guid': 'ccc', 'email': 'minnie@disney'}, ] 我需要做一个聚合查询,我要search包含字母“o”的email 。 […]

mongodb将聚合输出写入集合,而不会清除以前的数据

我有一个user集合和一个job集合。 每当插入一个新的工作,我正在运行一个聚合pipe道查询,输出一个文件列表,我需要为我的未来工作。 用$out操作符我可以写信给我们说match集合。 现在无论何时插入新作业并且查询再次运行,新的输出都将清除所有以前的match集合的数据。 我不想要,但根据MongoDB中logging的工作。 为了使我的数据持久化,我可以在我的应用程序中调用toArray() ,然后写入一个新的集合。 但据我所知,这不会扩展到1万到1万个文件。 我的问题是,我可以(可能吗?)解决这个特定的问题,而不会给内存带来太多的负担? 任何像bulkInsert的游标? 有点延迟的计算不会伤害太多 这是我的pipe道的结构。 // … previous stages const stage7 = { $out: 'match' } …. collection.aggregate( [stage1, stage2, stage3, stage4, stage5, stage6, stage7], (err, result) => { if (err) { console.log('agg failed error ', err) throw new Error('aggregation failed to write in match db', err) } […]

使用mongodb聚合框架计算频率

我试图根据10秒的间隔计算我的数据库中的文件的频率。 这是我的数据库对象的样子: [ { created_at: "2014-03-31T22:30:48.000Z", id: 450762158586880000, _id: "5339ec9808eb125965f2eae1" }, { created_at: "2014-03-31T22:30:48.000Z", id: 450762160407597060, _id: "5339ec9808eb125965f2eae2" }, { created_at: "2014-03-31T22:30:49.000Z", id: 450762163482017800, _id: "5339ec9908eb125965f2eae3" }, { created_at: "2014-03-31T22:30:49.000Z", id: 450762166367707140, _id: "5339ec9908eb125965f2eae4" }, { created_at: "2014-03-31T22:30:50.000Z", id: 450762167412064260, _id: "5339ec9a08eb125965f2eae5" } ] 我已经设法在给定的时间间隔内显示频率,但我希望每10秒钟就能得到一次。 所以最好我的JSON看起来像: [ { time_from: "2014-03-31T22:30:48.000Z", time_to: "2014-03-31T22:30:58.000Z", count: 6 }, […]

挑战:从input元素聚合第二元素

对MongoDB来说,这是一个挑战,我觉得很有趣。 给定一个时间戳集合events和一个特定的inputselect器_object ,我们如何汇总一个input后面的event文档列表? 例如,Mongoose中的Schema: var EventSchema = new Schema({ _object: { type: ObjectId } , timestamp: { type: Date, default: Date.now } }); 示例集合: [ { _id: ObjectId('1'), _object: ObjectId('123abc…1', timestamp: 'Sat Jun 21 2014 16:30:01 GMT-0400 (EDT) ) } , { _id: ObjectId('2'), _object: ObjectId('123abc…2', timestamp: 'Sat Jun 22 2014 16:30:00 GMT-0400 (EDT) ) […]

Mongodb聚合pipe道新的date

我尝试使用聚合pipe道来添加/创build新的date根据以前的pipe道值,并保存到一个新的集合(见我的pipe道下面)。 但是,语法是错误的,我得到一个错误说 不允许的字段types对象expression式中的date(在'date')//date:new Date('$ _ id.year','$ _id.month','$ _id.day') 我想知道如何使用我在mongo聚合pipe道中的上一年,每月,每天的值来创build新的date? 基本上,在将ISODate转换为分组的年,月,日之后,我想将它们转换回ISODate格式。 pipeline = [{ $match: { event: 'sample_event', } }, { $project: { _id: false, uuid: '$properties.distinct_id', time: '$properties.time' } }, { $group: { _id: { year: { $year: '$time' }, month: { $month: '$time' }, day: { $dayOfMonth: '$time' }, uuid: '$uuid' } } }, […]

聚合工作在控制台,但不是节点?

我对node.js(也是Javascript)完全陌生,所以匿名函数,callback函数和类似的想法对我来说是一个完全陌生的东西,而且我还在为这个包裹着…所以请说容易在我身上! 🙂 我有以下查询我可以在mongo控制台中使用,没有任何问题: db.warmod_events.aggregate({ $match: { $and: [ { "match.id": 1 }, { "match.event.player.name": 'xxx' }, { "match.event.event": 'round_stats' } ] } }, { $group: { _id : null, kills : { $sum: "$match.kills" } } }); 从阅读mongo节点驱动程序文档(这似乎有点简短),这是我想出的,但由于某种原因,它永远不会返回任何东西,查询结果总是空的,我不知道为什么? var getKills = function (db, callback) { var collection = db.collection('warmod_events'); collection.aggregate( [ { $match: { and: […]