mongoskin dynamic collection name error

744 views Asked by At

I need to insert into a Mongodb collection where the collection name is inside a variable. but it seems its not possible or my code is wrong.

db.collection(colname).insert(dbjson, function(err, result) {
    if (err) throw err;
    if (result) console.log('Added!');
});

but it throw this error:

Error: collection name must be a String
    at Error (<anonymous>)
    at checkCollectionName

any idea on how could i deal with this error? thanks

1

There are 1 answers

0
Neophile On BEST ANSWER

try this

db.collection(colname.toString()).insert(dbjson, function(err, result)