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

2

3

My goal is to use MongoDB's (2.4.4) text command from Node. It works fine from the command line. Based on this previous SO issue: Equivalent to mongo shell db.collection.runCommand() in Node.js, I tried using MongoJS (0.7.17) but can't make it go. Here is the code:

mongojs = require('mongojs');
var products = mongojs('localhost:27017/mydb').collection('products');
products.runCommand('text', {search: 'a'}, function (err, docs) {
   ...
});

docs returns undefined and err is null. I can execute a normal function such as products.find() fine... and I can execute the search on the MongoDB command line. Anyone know how to make this go?

BTW, here is what docs contains in the callback:

{
    "queryDebugString": "||||||",
    "language": "english",
    "results": [],
    "stats": {
        "nscanned": 0,
        "nscannedObjects": 0,
        "n": 0,
        "nfound": 0,
        "timeMicros": 55
    },
    "ok": 1
}

BTW, if there's another approach to make this work with just the normal native driver, I'm fine with that.

Ondrea answered 27/8, 2013 at 0:43 Comment(0)
B
6

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.

Beseech answered 27/8, 2013 at 6:37 Comment(4)
Yes, although the mongojs docs say that should work. I just ended up with a variant of what you proposed... running off of the database instead.Thanks. It looks like: db.command({ text: 'products', search: 'mysearchterm' }, function(e, o) {});Ondrea
Can someone explain the difference between using find and using the db.command? Why can't I use the text index with find. Wouldn't that follow the convention a lot better?Gerik
I am getting db.command is not a function, I have mongodb 3.0.6 and getting the same result with runCommand as wellDrive
@NaguibIhab thats because you have not included db name in the url. For eg: if you are using database url to connect as host:port it will give error so correct way is to either use url as host:port/dbName or use variable let DB = db.db(dbName); DB.command({})Ceria
N
-1

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

        }
});
Navar answered 9/4, 2019 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.