0

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

max imax
  • 2,131
  • 3
  • 15
  • 16

1 Answers1

1

try this

db.collection(colname.toString()).insert(dbjson, function(err, result) 
Neophile
  • 1,519
  • 12
  • 15