Tag: 聚合框架

在mongodb中排除子文档数组中的字段

我有两个文件 image文件: {_id:123,user:{user_sub_docuemnt},thumb:'dqwdqwdqwdw'} post文档: {_id:444, user:{user_sub_document}, attach:[{_id:123, user:{user_sub_docuemnt}, thumb:'dqwdqwdqwdw'} ] } user_sub_document包含password字段,所以我需要排除该字段。 这是我迄今为止: Post.aggregate([ {$match: {'user._id': {$in:idArr}}}, {$project:{content:1,attach:1,pub_date:1,'user.avatar':1}}, ],function(err,posts){ if(err){ throw err }else{ res.send(posts) } }) 这只会限制Post级别的用户,在attach数组中还有另一个user_sub_document,所以我试了这个 {$project:{content:1,attach:1,'attach.user':0,pub_date:1,'user.avatar':1}}, 这会给我一个错误The top-level _id field is the only field currently supported for exclusion 请在这件事上给予我帮助!

TypeError:无法在node.js中调用未定义的方法“toArray”,而在aggregatein mongo中

在node.js中进行聚合时出现以下错误。 错误:TypeError:无法调用未定义的方法“toArray” doctorsCollection.aggregate([ {$project:{"treatments._id":1, "treatments.price":1}}, {$unwind:"$treatments"}, {$match:{"treatments._id": parseInt(treatments[i])}}, {$sort:{"treatments.price":-1}}, {$limit:1} ]).toArray(function(err, result) { console.log(err); }); 我不知道发生了什么事。 任何帮助,将不胜感激。 谢谢!

使用objectIds数组查找文档

我有一个objectIds数组表示为string。 ["594c7a30f8f774268a2cdd14","594c7a30f8f774268a2cdd13"] 在我的聚合中,我dynamic地用这个数组和其他字段一起构build一个匹配对象。 match["_id"] = { $in: classes}; 这是行不通的,因为它只是一个string数组,而不是像ObjectId这样的objectID(“594c7a30f8f774268a2cdd14”)。 如何build立匹配,以便_idsearch与数组一起工作?

MongoDB:聚合,由几个字段组成

我有一个收集文件的结构 { type: 'a', date: '2014-01-04' }, { type: 'b', date: '2014-01-04' }, { type: 'b', date: '2014-01-04 }, { type: 'c', date: '2014-01-03' }, { type: 'a', date: '2014-01-03' } 我想通过date和types来聚合这些数据(按日​​期分组并按types计数): { date: '2014-01-04': { 'a': 1, 'b': 2 }, date: '2014-01'03': { 'a': 1, 'c': 1 } } 我有这样的聚合函数 db.items.aggregate([ { $match: { […]

避免总计16MB的限制

我有一个约1M文件的集合。 每个文档都有internalNumber属性,我需要在我的node.js代码中获得所有的internalNumber 。 以前我在用 db.docs.distinct("internalNumber") 要么 collection.distinct('internalNumber', {}, {},(err, result) => { /* … */ }) 在节点。 但随着collections的增长,我开始得到这样的错误: distinct is too big, 16m cap 。 现在我想要使用聚合。 它消耗了大量的内存,而且速度很慢,但是这样可以,因为我只需要在脚本启动时只执行一次。 我已经尝试过Robo 3T GUI工具: db.docs.aggregate([{$group: {_id: '$internalNumber'} }]); 它的工作原理,我想在node.js中使用它的代码如下: collection.aggregate([{$group: {_id: '$internalNumber'} }], (err, docs) => { /* … * }); 但在节点中,我得到一个错误: "MongoError: aggregation result exceeds maximum document size […]

在MongoDB中获取数组中的随机元素

这是我的数据库结构 { "_id" : ObjectId("576155226d1d298c2cc3edca"), "questionLibrary" : { "technologyName" : "CSS", "questions" : [ { "correctanswer" : { "A1" : "CSS1" }, "answeroption" : { "A4" : "CSS1", "A3" : "CSS1", "A2" : "CSS1", "A1" : "CSS1" }, "level" : "Amature", "tags" : "CSS", "question" : "CSS1" }, { "question" : "CSS2", "tags" : "CSS", […]

MongoDB节点驱动程序当前聚合的计数

我正在使用mongodb的节点,并试图聚集基于一些设置的filter的文件集合,然后将其限制为10.我有它聚合就好,限制就好了,但我需要得到聚合的文件总数之前我限制他们到10。 这是我的代码。 var qry = []; if (filter.FocusArea && filter.FocusArea != "(None)") { qry.push({ $match: { 'ProgramAreaId': filter.FocusArea } }); } if (filter.Status && filter.Status != "(None)") { qry.push({ $match: { 'StatusId': filter.Status } }); } if (filter.ProgOfficer && filter.ProgOfficer != "(None)") { qry.push({ $match: { 'ProgramOfficerId': filter.ProgOfficer } }); } if (filter.Fund && filter.Fund […]

获得两个不同结构的集合的区别

假设我有两个集合, A和B A包含以下forms的简单文档: { _id: '…', value: 'A', data: '…' } { _id: '…', value: 'B', data: '…' } { _id: '…', value: 'C', data: '…' } … B包含这样的嵌套对象: { _id: '…', values: [ 'A', 'B' ]} { _id: '…', values: [ 'C' ]} … 现在可能发生的情况是, A中有文档没有被B的任何文档引用,或者B中有引用的文档在B中不存在。 我们称他们为“孤儿”。 我现在的问题是:如何以最有效的方式find那些孤儿文件? 最后,我需要的是他们的_id领域。 到目前为止,我已经使用unwind来“扁平化” A ,并使用lambda的differenceWith函数来计算差异,但这需要相当长的时间,并且肯定不是很有效,因为我在客户端上完成所有工作,而不是在数据库。 我已经看到,在MongoDB中有一个$setDifference运算符,但我没有得到它的工作。 任何人都可以指出我正确的方向,如何使用Node.js解决这个问题,并在数据库中运行大部分(所有?)工作? […]

mongodb聚合框架嵌套数组减去expression式

我有深嵌套数组,我试图通过一些嵌套数组元素,并得到这个工作。 但是,当我尝试使用$减法expression式,它失败。 任何指针赞赏。 Data: -scenes: [ -{ name: "Greeting_Excited" -records: [ – { type: "listeningCycle" listeningId: 2 timestamp: 1354566662041 -events: [ … ] -timeProfile: { -timeStampInfo: { earliestStamp: 1354566664530 latestStamp: 1354566678412 } -timing: [ -{ start: 400 stop: 556 id: "SR-G" } -{ start: 559 stop: 572 id: "NL-G" } ] } } ] } […]

Node.js – TypeError:无法读取未定义的属性'readPreference'

我正在尝试在我的Node程序中使用MongoDB的聚合方法时出现types错误。 我正在使用节点0.10.16版本和mongodb mongodb-win32-x86_64-2.4.2版本。 这里是我在MongoDB中的“关系”表的细节 db.relation.find()。的forEach(printjson) { "_id" : ObjectId("518808811189128048034c7b") } { "username" : "51880fa37901642c36000001", "selectedoption" : "RCB", "question" : "Which team is going to win IPL6?", "_id" : ObjectId("5188137dee3a599c34000001") } { "username" : "51880fa37901642c36000001", "selectedoption" : "Mumbai", "question" : "Which team is going to win IPL6?", "_id" : ObjectId("518c2bd0afa8d2e82b000001") } { "username" : "51880fa37901642c36000001", "selectedoption" […]