A
A
Alexander Arkhangelsky2020-08-09 01:59:36
JavaScript
Alexander Arkhangelsky, 2020-08-09 01:59:36

Error when using $switch statement, where is the error?

People, I ask for help. Recently I took up mongo and there is such an error, I seem to do everything according to the documentation, but nothing comes out ...

Error:
5f2f2ddca84de520645344.png

Scheme:

const mongoose = require('mongoose');
const users = mongoose.Schema({
    vkid: Number,
    pers: {
        lucky: Number
    }
});
module.exports = mongoose.model('Users', users);

The code:
await users.updateMany({ vkid: id }, {
                $set: {
                    "pers.lucky": {
                        $switch: {
                            branches: [
                                { case: message.messagePayload.command === 'первый', then: 1 },
                                { case: message.messagePayload.command === 'второй', then: 2 },
                                { case: message.messagePayload.command === 'третий', then: 3 }
                            ], default: 69
                        }
                    }
                }
            });


In the code itself, several objects need to be replaced, I just cited such code as an example, but the error is the same.

upd: when I write like this, everything works:
await users.updateMany({ vkid: id }, {
                    $set: {
                        "pers.lucky": 5
                    }
                });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fostsinger, 2020-08-12
@Wertaer

you need to use the $eq operator instead of ===

await users.updateMany({ vkid: id }, {
                $set: {
                    "pers.lucky": {
                        $switch: {
                            branches: [
  {
                        case: {$eq: ["$message.messagePayload.command",  'первый',]},
                        then: 1
                    }, ], default: 69
                        }
                    }
                }
            });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question