Search code examples
node.jsmongodbmongoskin

mongoskin dynamic collection name error


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


Solution

  • try this

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