Frage

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

War es hilfreich?

Lösung

try this

db.collection(colname.toString()).insert(dbjson, function(err, result) 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top