2013-09-01 32 views
7

Ich erhalte einen Typfehler beim Versuch, die aggregierte Methode von mongodb in meinem Node-Programm zu verwenden. Ich benutze Knoten 0.10.16 Version und mongodb mongodb-win32-x86_64-2.4.2 Version.Node.js - TypeError: Kann die Eigenschaft 'readPreference' von undefined nicht lesen

Hier sind die Details meiner 'Beziehung' Tabelle in 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") 
} 

Diese alle groben Daten in obiger Tabelle ist (Sammlung in mongodb). Ich brauche eine 'Gruppe von' in SQL-Terme auf der obigen Tabelle auf der Basis der Spalte 'selectedoption' und brauche nur eine Zählung. Das Ergebnis wird also auf 'RCB', 'CSK', 'Mumbai' Daten zählen. Hier ist mein Beispielknoten Schnipsel, entfernt un-nötig Linien, ich bin mit Express Server -

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'); 
    }); 
}); 

Die obige Linie -

collection.aggregate({$group : {_id: "$selectedoption", selectedoption: {$sum: 1}}})

gibt mir die folgenden Fehler in Knoten Konsole

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) 

- Es läuft gut in der Mongodb-Konsole und gibt mir folgende Ausgabe -

> 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 
} 

Ich würde wirklich alle Hinweise oder Hinweise zur Lösung dieses Problems zu schätzen wissen.

Antwort

11

Sie benötigen einen Rückruf functionlike dies zu implementieren:

collection.aggregate({$group : {_id: "$selectedoption", selectedoption: {$sum: 1}}}, 
function(err, result){ 
    //some stuff 
}); 
Verwandte Themen