Marin Leontenko
Marin Leontenko

Reputation: 771

MongoDB script fails to load, but code works in Mongo shell

I have the following script (script.js):

    categoricalVars = db.frekvencija_ctg.find().toArray();
​
    db.ctg.find().forEach(e => {
        db.emb_ctg.insert({ ...e, frekvencija: categoricalVars });
    });

When I try to load it in Mongo shell via load("script.js"), I get the following error:

[js] SyntaxError: illegal character :

If I run these expressions in Mongo shell one by one with copy/paste, they work and I get desired result. What is wrong in script?

Upvotes: 2

Views: 636

Answers (2)

sudo97
sudo97

Reputation: 914

I have got this error before and I have resolved this by removing the braces from the arrow function passed to the forEach() method. Try changing your script to something like this

categoricalVars = db.frekvencija_ctg.find().toArray();
​
db.ctg.find().forEach(function(e){
    var obj = {...e};
    obj.frekvencija = categoricalVars;
    db.emb_ctg.insert(obj);
});

Upvotes: 1

Renan Le Caro
Renan Le Caro

Reputation: 365

Seems like you're using an old version of node that doesn't support fancy syntax. Either udgrade node or use old school syntax like :

    var categoricalVars = db.frekvencija_ctg.find().toArray();
​
    db.ctg.find().forEach(function(e){
        db.emb_ctg.insert(Object.assign({},e, {frekvencija: categoricalVars }));
    });

Upvotes: 1

Related Questions