MongoDB查询可以从shell运行,但是NodeJS驱动程序失败
我有一个名为“文件”的集合与这些文件:
{ "_id" : "8e1e0d87-fa8f-4c0a-bd7f-78f1b84aefbb", "groupId" : "7ee973c0-54b5-11e4-aaed-0002a5d5c51b", "uploadDate" : NumberLong(1427392635983), "file" : "n/ws/hs/f25a2613-3094-4150-9268-c6248475218d", "length" : 56353, "contentType" : "image/other" } /* 2 */ { "_id" : "de392172-27a5-45bb-ba11-d1c494847dda", "groupId" : "7ee973c0-54b5-11e4-aaed-0002a5d5c51b", "uploadDate" : NumberLong(1427392687008), "file" : "y/ba/co/22c987c2-2ab3-46a3-8ff8-474a070f2fcc", "length" : 56353, "contentType" : "image/other" }
我想通过7ee973c0-54b5-11e4-aaed-0002a5d5c51b
驱动程序7ee973c0-54b5-11e4-aaed-0002a5d5c51b
groupId
7ee973c0-54b5-11e4-aaed-0002a5d5c51b
所有文档长度字段值的总和。
我在Mongo Shell中试过这个查询:
db.files.aggregate( [ {$match:{"groupId":"7ee973c0-54b5-11e4-aaed-0002a5d5c51b"}}, { $group: { "_id": "test", "totalLength":{$sum:"$length"} } } ] )
它的工作。 我试图在NodeJS方法中做同样的事情:
function(db, callback) { var groupId = "7ee973c0-54b5-11e4-aaed-0002a5d5c51b"; console.log("*****sending query"); db.collection('files').aggregate( [ {$match:{"groupId":groupId}}, { $group: { "_id": "test", "totalLength":{$sum:"$length"} } } ]).toArray(function(err, result) { console.log("***received"); console.log(result); callback(result); } ); }
这不起作用。 它在打印******发送查询后突然停止,没有进一步的错误信息或任何types的东西。 我不确定这里有什么问题。 任何帮助是极大的赞赏。
- 通过node.js完成对Mongodb的asynchronous请求
- 为什么我会连接错误尝试连接到mongo
- Ubuntu中包含Express,MongoDB和Mongoose的Node.js中的REST API。 MongoError:第一次连接时无法连接到服务器
- 在mongoosedbstream行的自动计数器
- mongooseForegin键映射
- findOneAndUpdate和push数组元素给mongoose带来错误
- 如何用一个请求更新数组对象的多个字段?
- Node.jsfile upload(Express 4,MongoDB,GridFS,GridFSstream)
- jQueryselect在button上使用“this”