MongoDB and MongoJS - can't get runCommand to work for text queries

无人久伴 提交于 2019-12-06 00:47:12

Using the native driver I can run a command off of the db object as follows:

var MongoClient = require("mongodb").MongoClient;
MongoClient.connect(database, function (err, db) {
    if (!err) {
        db.command({ distinct: "Messages", key: "session" }, function (err, result) {
            //more code here
        });
    }
});

I noticed you are running the command off of the collection object, that might be the problem.

Yo need to call the command inside the db object, not inside the connect object

MongoClient.connect(url, function (err, db) {
    if (!err) {
        var dbo = db.db();
        dbo.command({ insert: "mycollection", documents: [{"test":1}]}, function 
(err, result) {

            if (err)
            {
                console.log(err);
            }else
            {
                console.log("1 document inserted",result);
            }
            }
        );

        }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!