3

It's different that the result of mongoShell and node.js application. My mongodb environment uses shard cluster.

The result of mongoShell

mongos> db.runCommand({aggregate : "collection", pipeline : my_pipeline(), allowDiskUse : true});
{
    "result" : [ ],
    "ok" : 1,
    "$gleStats" : {
        "lastOpTime" : Timestamp(1428399959, 408),
        "electionId" : ObjectId("552363d7ddfce783509094e5")
    }
}

The result of node.js application

var mongodb = require('mongodb');
var mongoClient = new mongodb.MongoClient(...);
mongoClient.open(function (err, mongoClient) {
    var db = mongoClient.db(...);
    db.command({aggregate : "collection", pipeline : my_pipeline(),      allowDiskUse : true}, function (err) {
        ...
    });
});

-->

MongoError: exception: failed to create temporary $out collection 'db.tmp.agg_out.12': { note: "from execCommand", ok: 0.0, errmsg: "not master" }

I want to execute aggregation framework query in node.js application.

How can I execute this query in application?

John Conde
  • 217,595
  • 99
  • 455
  • 496
ehkim1440
  • 71
  • 6
  • I think you have to set "slave okay" mode in the shell by `rs.slaveOk()`. This lets the mongo shell know that you're allowing reads from a secondary and you can query normally from secondaries. – chridam Apr 07 '15 at 11:34
  • 1
    The reason for this is that you have allowdiskUse set to true and the aggregation pipeline is running on a secondary. You cannot write to secondaries in MongoDB. What does your connection code look like? Like the bit in MongoClients constructor – Sammaye Apr 07 '15 at 11:54

1 Answers1

3

I resolved it myself through upgrading node.js mongodb driver.

I used mongodb driver that is version 1.4.29 in previous test.

In 1.4.29 version API, 'db.command' doesn't check read preference about slaves.

So when I run my application, I got a error like that.

After upgrading node.js mongodb driver to 2.0.27(latest) and custormizing my application to changed API, the application runs very well.

Thank you for your suggestions.

ehkim1440
  • 71
  • 6