Tag: 聚合框架

MongoDB超时问题(Node.js)

我在Node.JS有一个应用程序,基本上每30分钟从WebAPI检索一个数据的快照。 当我尝试通过使用聚合查询来询问数据库时,应用程序触发与超时有关的错误(MongoError:连接1到127.0.0.1:27017超时)。 从我的日志,我看到它正好是30秒。 聚合查询是这样的: collection.aggregate([{ "$group": { "_id": { country: "$country", user: "$user" } } }]). 我在另一个客户端(但是同一个数据库)上testing了这个查询,并且需要大约60秒钟来处理25.000.000条logging。 我的假设是有一个超时参数的值为30秒阻止我的应用程序完成查询。 有人可以告诉我如何在Node.JS中设置此参数吗? 我正在使用'mongodb'js库,我的主机是Windows。 由于这是一个个人项目,我想调查如何使用MongoDB而不是更传统的解决scheme(如Oracle或MySQL),但由于分析是整个应用程序的核心部分,因此这可能是一个难题。

MongoDB集合汇总了多个对象

鉴于以下收集: { "_id" : ObjectId("57bb00b1471bcc08e819bff3"), "BCNED3351" : { "timestamp" : 1471873201170.0, "totalOID" : { "backlog" : 1405, "inflow" : 396, "handled" : 341 }, "queues" : { "12" : { "backlog" : 5, "inflow" : 0, "handled" : 0 }, "30" : { "backlog" : 124, "inflow" : 1, "handled" : 1 }, "31" : […]

NodeJS MongoDB传递参数在聚合pipe道中

如何将parameter passing给聚合? 我得到的参数,并试图传递它使用$match运算符,但查询返回空数组: app.get('/api/:name', function(req, res){ var name = req.params.name; console.log(name); db.collection('coll').aggregate([{$match: {name: '$name'}}, {$unwind: { path: "$dates", includeArrayIndex: "idx" } }, { $project: { _id: 0, dates: 1, numbers: { $arrayElemAt: ["$numbers", "$idx"] }, goals: { $arrayElemAt: ["$goals", "$idx"] }, durations: { $arrayElemAt: ["$durations", "$idx"]}}}]).toArray(function(err, docs) { if (err) { assert.equal(null); } else { […]

MongoDB聚合:$存在的任何值

我试图利用MongoDB聚合RESTful api,但陷入了下面的情况。 假设我有Subscriptions模型,看起来像这样: var mongoose = require('mongoose'), Schema = mongoose.Schema; var SubscriptionSchema = new Schema({ // … cancelled: Date }); 如果订阅处于活动状态,则cancelled属性可以是undefined ,也可以是用户取消操作的Date 。 现在,我有一个GET /me/subscriptions ,它聚合了订阅,并有一个可选的查询参数: cancelled=true (只显示取消)或cancelled=false (只显示活动)。 如果没有指定,应该返回任何订阅(激活或取消)。 var express = require('express'), router = express.Router(), Subscription = require('../../models/subscription'); router.get('/me/subscriptions', function(req, res, next) { var cancelled = req.query.cancelled === 'true' ? { $exists: true […]

如何在查询父项时获取聚合的mongoose子文档数组中的值的总和?

我正在尝试在express和mongoose之上构build一些先进的Hello World应用程序。 假设我有下一个Schema: const pollOptionsSchema = new Schema({ name: String, votes: { type: Number, default: 0 } }); const pollSchema = new Schema({ name: String, dateCreated: { type: Date, default: Date.now }, author: { type: Schema.Types.ObjectId }, options: [pollOptionsSchema] }); 而当我只是打电话 Poll.findOne({_id: req.params.id}).exec((err, data) => { if (err) console.log(err); // I receive next data: // […]

有条件地包含聚合stream水线阶段

我有一个函数,根据给定的参数给我一些命令。 但是,参数可以是空的,在这种情况下,我想单独离开$match 。 这是我现在的代码: if(req.query.status && typeof(req.query.status) == 'array'){ var match = { $in: req.query.status }; }else if(req.query.status){ var match = req.query.status; }else{ //when empty find all statuses var match = null; } Order.aggregate( { $match: { 'shop.nameSlug' : req.query.nameSlug, } }, { $unwind: "$status" }, { $match: { "status.status" : match } }, { […]

如何计算mongoDB中唯一数据的数量

我在mongoDB中是非常新的初学者。 我们必须显示输出数据 用户按date的唯一编号。 Android用户和Ios用户的数量。 这是inputJSON数据 input [{ 'name' : 'user1', 'date' : '23/09/2017', 'deviceType' : 'Android' }, { 'name' : 'user2', 'date' : '24/09/2017', 'deviceType' :'ios' }, { 'name' : 'user1', 'date' : '23/09/2017', 'deviceType' :'ios' }, { 'name' : 'user2', 'date' : '23/09/2017', 'deviceType' :'ios' }, { 'name' : 'user1', 'date' : '24/09/2017', 'deviceType' […]

来自外部集合的项目特定字段$查找结果

我正在写聚合,以获得与本地收集外国收集数据。 db.getCollection('orders').aggregate([ { $match: { status: "UNASSIGNED", serviceLocationId: "83177" } }, { $lookup: { from: "servicelocations", localField: "serviceLocationId", foreignField: "serviceLocationId", as: "locations" } }, { $unwind: "$locations" }]) 我越来越: { "_id" : ObjectId("59d32b5c360198e441b67545"), "accountId" : 1.0, "orderId" : "AQ137O1701240", "serviceLocationId" : "83177", "orderDate" : "2017-09-18T18:29:00.000Z", "description" : "AQ137O1701240", "serviceType" : "Delivery", "orderSource" : "Import", "takenBy" […]

Mongodb聚合与客户端处理

我有一个几乎具有以下模式的blogs集合: { title: { name: "My First Blog Post", postDate: "01-28-11" }, content: "Here is my super long post …", comments: [ { text: "This post sucks!" , name: "seanhess" , created: 01-28-14} , { text: "I know! I wish it were longer" , name: "bob" , postDate: 01-28-11} ] } 我主要想运行三个查询: 给我所有的comments 只有 bob […]

按最高计数合计

如何使用聚合框架以最高计数来聚合结果? { "type": "dog", "name": "buddy", "count": 67 }, { "type": "dog", "name": "buddy", "count": 34 }, { "type": "dog", "name": "tucker", "count": 17 }, { "type": "dog", "name": "tucker", "count": 52 }, { "type": "cat", "name": "gizmo", "count": 11 } 我想汇总上面的结果,所以如果dog有相同的name我想得到最高的一个。 所以它看起来像: { "type": "dog", "name": "buddy", "count": 67 }, { "type": "dog", "name": […]