- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
尝试在我的 Node 程序中使用 mongodb 的聚合方法时出现类型错误。我使用的是 node 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" : "Mumbai",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("518c2c81681aae0838000001")
}
{
"username" : "51880fa37901642c36000001",
"selectedoption" : "RCB",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("518c2c90681aae0838000002")
}
{
"username" : "51880fa37901642c36000001",
"selectedoption" : "CSK",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("518c2cefc7dc797c08000001")
}
{
"username" : "51880fa37901642c36000001",
"selectedoption" : "Mumbai",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("518c2d48a2afbdc426000001")
}
{
"username" : "51880fa37901642c36000001",
"selectedoption" : "RCB",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("518c2e2fa2afbdc426000002")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "RCB",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fda9ea6abb63037000002")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "CSK",
"question" : "who will IPL this year?",
"_id" : ObjectId("521fdb3ba6abb63037000003")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "Mumbai",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fde8da6abb63037000004")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "CSK",
"question" : "who will IPL this year?",
"_id" : ObjectId("521fdef0a6abb63037000005")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "Mumbai",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fdf1aa6abb63037000006")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "RCB",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fdffe257b59cc3f000001")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "Mumbai",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fe09c827ff1e418000001")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "CSK",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fe0cb5022f0c03c000001")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "Mumbai",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fe1558b82555c25000001")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "CSK",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fe17d8b82555c25000002")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "CSK",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fe2e8c134b7bc40000001")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "CSK",
"question" : "who will IPL this year?",
"_id" : ObjectId("521fe354b59421cc07000001")
}
{
"username" : "521fda91a6abb63037000001",
"selectedoption" : "RCB",
"question" : "Which team is going to win IPL6?",
"_id" : ObjectId("521fe392b59421cc07000002")
}
这是上表中的所有粗略数据(mongodb 中的集合)。我需要在上表中基于“selectedoption”列的 sql 术语中的“分组依据”,只需要对其进行计数。因此,结果将是对“RCB”、“CSK”、“孟买”数据的计数。这是我的示例 Node 片段,删除了不需要的行,我使用的是快速服务器 -
var express = require('express'),
routes = require('./routes'),
user = require('./routes/user'),
http = require('http'),
path = require('path');
var app = express();
var mongo = require('mongodb');
var db = new mongo.Db('voteapp', new mongo.Server("127.0.0.1", 27017, {
auto_reconnect: true
}, {
safe: false,
strict: false
}))
app.get('/test', function(req, res) {
res.send('works');
db.collection('relation', function(err, collection){
console.log('in');
collection.aggregate({$group : {_id: "$selectedoption", selectedoption: {$sum: 1}}});
console.log('out');
});
});
上面一行-
collection.aggregate({$group : {_id: "$selectedoption", selectedoption: {$sum: 1}}})
在 Node 控制台中给我以下错误
TypeError: Cannot read property 'readPreference' of undefined at Collection.aggregate (D:\mongo-js - 1\node_modules\mongodb\lib\mongodb\collection.js:1632:17)
at D:\mongo-js - 1\app.js:107:16
at Db.collection (D:\mongo-js - 1\node_modules\mongodb\lib\mongodb\db.js:461:44)
at D:\mongo-js - 1\app.js:105:6
at callbacks (D:\mongo-js - 1\node_modules\express\lib\router\index.js:161:37)
at param (D:\mongo-js - 1\node_modules\express\lib\router\index.js:135:11)
at pass (D:\mongo-js - 1\node_modules\express\lib\router\index.js:142:5)
at Router._dispatch (D:\mongo-js - 1\node_modules\express\lib\router\index.js:170:5)
at Object.router (D:\mongo-js - 1\node_modules\express\lib\router\index.js:33:10)
at next (D:\mongo-js - 1\node_modules\express\node_modules\connect\lib\proto.js:190:15)
-它在 mongodb 控制台中运行良好,并给出以下输出 -
> db.relation.aggregate({$group : {_id: "$selectedoption", selectedoption: {$sum: 1}}});
{
"result" : [
{
"_id" : "CSK",
"selectedoption" : 9
},
{
"_id" : "Mumbai",
"selectedoption" : 10
},
{
"_id" : "RCB",
"selectedoption" : 8
},
{
"_id" : null,
"selectedoption" : 1
}
],
"ok" : 1
}
如果能提供解决此问题的任何指示或指导,我将不胜感激。
最佳答案
你需要像这样实现一个回调函数:
collection.aggregate({$group : {_id: "$selectedoption", selectedoption: {$sum: 1}}},
function(err, result){
//some stuff
});
关于Node.js - 类型错误 : Cannot read property 'readPreference' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18563562/
可能就在我面前,但我不确定如何在 C# 驱动程序中设置 ReadPreference.Secondary 设置?我想将查询负载分配到我的辅助节点,而不是默认的。我已经在节点本身上设置了 slaveOk
我阅读了很多 MongoDB 文档,但我无法理解 readConcern 和 readPreference 选项之间的区别。 例如:如果我在阅读关注选项中设置“多数”并将“主要”设置为我的阅读偏好选项
我有一个使用多个不同 mongo 数据库的应用程序,99% 的时间它需要从副本集的主数据库读取,但我有几个报告查询可以拉回大量数据,并且不在索引,我希望它们针对辅助节点运行。有什么方法可以为单个查询指
我有一个使用多个不同 mongo 数据库的应用程序,99% 的时间它需要从副本集的主数据库读取,但我有几个报告查询可以拉回大量数据,并且不在索引,我希望它们针对辅助节点运行。有什么方法可以为单个查询指
尝试在我的 Node 程序中使用 mongodb 的聚合方法时出现类型错误。我使用的是 node 0.10.16 版本和 mongodb mongodb-win32-x86_64-2.4.2 版本。
我无法诊断我的 Java 应用程序对 MongoDB 的请求没有路由到最近的副本的问题,我希望有人能提供帮助。让我先解释一下我的配置。 配置: 我正在生产中运行一个 MongoDB 实例,它是一个 S
我有一个在 Node/Meteor 中构建的文件存储服务,它使用 GridFS,并且跨多个容器进行复制。我目前正在尝试寻找的是,这段代码是否真的知道读/写一致性 db.command({ file
我正在通过 Spring MVC 中的 mongodb java 驱动程序连接到 MongoDB 分片服务器。我正在使用以下版本: spring-webmvc-3.2.1.RELEASE mongo-
问题 处理此类错误的最佳方法是什么?说完后重试,最多 30 秒。 5 次? 设置我在 AWS ECS 上有一个 PRIMARY + 两个 SECONDARY 设置,没有一个盒子被关闭。 从异常消息来看
我是一名优秀的程序员,十分优秀!