1

伙计们

在 node.js 中,不支持 mongolian 或 mongo-native 的 group 功能?

mongo本机代码,

var mongodb = require('mongodb');
var Db      = mongodb.Db;
var Server  = mongodb.Server;
var db  = new Db( "test", new Server( "localhost", 27017 ), { w:0 } );

db.collection( "user" ).group( {
    key: { },
    reduce: function ( curr, result ) { },
    initial: { }
} );

结果,

/node/ex1/node_modules/mongodb/lib/mongodb/collection.js:1400
      if(err != null) return callback(err);
                             ^
TypeError: undefined is not a function
    at Collection.group.scope (/node/ex1/node_modules/mongodb/lib/mongodb/collection.js:1400:30)
    at Db._executeQueryCommand (/node/ex1/node_modules/mongodb/lib/mongodb/db.js:1812:12)
    at Collection.group (/node/ex1/node_modules/mongodb/lib/mongodb/collection.js:1399:13)
    at Object.<anonymous> (/node/ex1/repository.js:34:25)

和蒙古语代码,

var Mongolian   = require( "mongolian" );
var server  = new Mongolian;
var db      = server.db( "test" );

db.collection( "user" ).group( {
    key: { },
    reduce: function ( curr, result ) { },
    initial: { }
} );

结果,

db.collection( "user" ).group( {
                        ^
TypeError: Object Mongolian[mongo://localhost:27017]/assistor.user has no method 'group'
    at Object.<anonymous> (/node/ex1/repository.js:77:25)
4

2 回答 2

3

如文档中所见,本机驱动程序支持它,但该group方法将key,reduce等作为单独的参数,而不是像 shell 那样的对象中的字段:

db.collection("user").group(
    {},
    {},
    { sum: 0 },
    function (curr, result) { },
    function (err, result) {
        // Process the result
    }
);
于 2013-01-19T14:57:43.927 回答
0

npm 的 mongolian 包没有更新,您必须克隆 git 存储库,然后将其与 npm 链接。

然后文档不完整,你必须做这样的事情,我花了一段时间才让它工作。

var Mongolian   = require( "mongolian" );
var server  = new Mongolian;
var db      = server.db( "test" );

db.collection( "user" ).group( {
    ns: "user",
    key: { },
    reduce: function ( curr, result ) { },
    initial: { }
   },function(error,post){
      if(error) console.log(error);
      //do something with post.retval
   }
);
于 2013-05-21T03:06:27.163 回答