2

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?

2 Answers 2

1

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 }));
    });
Sign up to request clarification or add additional context in comments.

1 Comment

I tried your code and it still returns the same error. I checked node version (typed node -v) in cmd and it says v12.13.1
1

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);
});

5 Comments

I tried your solution, but it still returns the same error.
Try js db.ctg.find().map(function(e){ db.emb_ctg.insert({...e, frekvencija: categoricalVars})});
Can you provide the full error log? It says there is an Illegal character : in the script. I only see it being used in the insert method for the frekvencija property of the object being inserted
I made another edit to the answer @Martin . Let me know if this works or not
I tried the last edit, but it still returns the same error. Where am I looking for log? I tried C:\Program Files\MongoDB\Server\4.2\log\mongod.log, but I don't see any errors related to this in there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.