Question

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

Was it helpful?

Solution

try this

db.collection(colname.toString()).insert(dbjson, function(err, result) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top