Tag: 聚合框架

如何使用聚合函数来计算mongoDB中的唯一数据

这是input的JSON数据这样存储在mongoDB中 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' :'ios' }, { 'name' […]

MongoDB不能调用未定义的方法'聚合'

我正试图拉数组中的最低分数。 我的错误是不能调用未定义的方法'聚合'。 聚合方法在mongodb中定义,我甚至可以定义它,而db.students也是定义的。 我似乎无法find我错在哪里。 var MongoClient = require('mongodb').MongoClient // Driver for connecting to MongoDB MongoClient.connect('mongodb://localhost:27017/school', function(err, db) { if(err) throw err; x = db.students.aggregate( [{ "$unwind":"$scores" },{"$match":{"scores.type":"homework"}},{ "$group":{"_id":"$_id","minscore":{"$min":"$scores.score" }}} ] ) x.result.forEach(function(doc) { db.students.update( { "_id": doc._id }, { "$pull": { "scores" : { "score":doc.minscore, "type":"homework" } } } ); }); }); 我有这种感觉,因为盯着这个太长时间了,我感到用一双新的眼睛来看这个东西。

未捕获的MongoError:无法识别的字段'allowDiskUsage'

我安装了2.5.5,以便我可以尝试新的“$ out”操作符来创build具有聚合结果的新集合。 我的节点适配器是mongodb@1.3.23。 我没有“allowDiskUsage”在我的代码,但我得到这个错误: Uncaught MongoError: unrecognized field 'allowDiskUsage' 我需要做什么来更新我的项目运行2.5.5?

Mongo:分组,推送和sorting

我有一个MongoDB收集数据,按照以下;我想根据EmployeedID(即0001),然后sorting(按年龄) { "_id" : ObjectId("54d0512191a4da7736e9db43"), "EmployeeID" : "0001", "Speciality" : "xxx", "Code" : "P", "Age" : 8 } /* 1 */ { "_id" : ObjectId("54d0512191a4da7736e9db44"), "EmployeeID" : "0002", "Speciality" : "yyyyy", "Code" : "P", "Age" : 6 } /* 2 */ { "_id" : ObjectId("54d0512191a4da7736e9db45"), "EmployeeID" : "0001", "Speciality" : "zzz", "Code" : "P", "Age" […]

Mongodb如何查询特定文档的数组子集

我想运行一个查询来select特定的文件。 然后在每个文档上打开一个子文档数组并运行一个查询来过滤这些子文档。 例: { "_id" : ObjectID(23412351346435), "list" : [ {date: ISODate(2015-01-12T00:00:00.000Z), name: "Jan 12"}, {date: ISODate(2015-01-13T00:00:00.000Z), name: "Jan 13"}, {date: ISODate(2015-01-14T00:00:00.000Z), name: "Jan 14"} ] } 我猜测我可以用Mongo的aggregate函数做一些事情。 我已经能够匹配我想要的文件,但我怎么能得到一个子查询进入arrays? 我尝试使用$elemMatch但只返回匹配date范围的数组中的第一个项目。 当我查询ObjectID(23412351346435)和date范围2015-01-12到2015-01-13要清楚我希望它返回这个; { "_id" : ObjectID(23412351346435), "list" : [ {date: ISODate(2015-01-12T00:00:00.000Z), name: "Jan 12"}, {date: ISODate(2015-01-13T00:00:00.000Z), name: "Jan 13"} ] }

带有节点驱动程序的Mongo游标

我正在使用本地驱动程序v2.1为mongo写一个聚合函数。 我的代码看起来像这样: db.collection("whatever").aggregate(…).each(function(err, doc) { // cursor processing }) 我的问题是:在哪里执行游标处理? 在客户端或服务器上? 我假设它是在客户端(节点)执行的,如果是这样的话,有没有办法在服务器上运行游标处理(或其他types的数据处理)? 我正在处理大量的数据,而且我不想与mongo服务器来回传输。 谢谢!

获取错误“pipe道元素3不是对象错误”,而试图使用聚合查找和更新

我正在使用node js mongodb driver &试图更新文档中的object array内的对象数组。 文档集合的模式是这样的: 我想要的是 : 对于order no = 1 & items.qty=2 & tax rate = 25 ,将tax to "cst" & taxratetype to "flat"更新tax to "cst" & taxratetype to "flat" 。 我试过的: db.OrderInfo.aggregate( {$match:{"orderno":"1"}}, {$unwind:'$items'}, { $match: { 'items.qty' : 2} },function(err,result1){ if(err){ throw(err); }else{ indexes = result1[0].items.taxes.map(function(obj, index) { if(obj.taxrate == […]

如何通过嵌套在多个数组中的_id来访问对象

新的MEAN.js / Mongo开发人员在这里,我真的很感激任何见解我可以在这个问题上得到。 这是我正在使用的数据的相关部分,从文档的顶部。 我试图深入到暂停数组中的对象的_id字段,以便我可以将它匹配到给定的值(以访问特定的实例)并对其他属性执行更新。 { "_id" : ObjectId("575c902078c8db620d50699e"), "username" : "superadmin", "poms" : [ { "_id" : ObjectId("577a6c6c1d4ddcf805e6a4a0"), "pauses" : [ { "_id" : ObjectId("577a6c6e1d4ddcf805e6a4a1"), "momentResumed" : null, "momentPaused" : ISODate("2016-07-04T14:02:22.643Z") }, { "_id" : ObjectId("577a6cae1d4ddcf805e6a4a2"), "momentResumed" : null, "momentPaused" : ISODate("2016-07-04T14:03:26.364Z") } ], "momentCompleted" : null, "momentStarted" : ISODate("2016-07-04T14:02:20.383Z") }, { "_id" : […]

按$ geonear汇总

我有一个看起来像这样的集合: [ { "roadname": "foo", "data": [ { "val": 50, "loc": { "type": "Point", "coordinates": [3.197033554, 50.64611712] } } }, { "val": NULL, "loc": { "type": "Point", "coordinates": [3.197740735, 50.6460058] } } } ] }, { "roadname": "foo", "data": [ { "val": 50, "loc": { "type": "Point", "coordinates": [3.32456512, 50.2744516] } } } ] }, […]

Mongoose中的子文件总和

目前我有这个模式。 var cartSchema = new Schema({ userPurchased: {type: Schema.Types.ObjectId, ref: 'users'}, products: [ { product: {type: Schema.Types.ObjectId, ref: 'products'}, size: {type: String, required: true}, quantity: {type: Number, required: true}, subTotal: {type: Number, required: true} } ], totalPrice: {type: Number, default: 0, required: true} }); 数据库logging的例子 { "_id": { "$oid": "586f4be94d3e481378469a08" }, "userPurchased": { "$oid": "586dca1f5f4a7810fc890f97" […]