Search code examples
javascriptmongodbbooleanmongodb-querynode-mongodb-native

Programmatically Toggle Boolean Value with Mongodb Native Driver


Im trying to figure out how to toggle the boolean value of "active" in the example from true to false or false to true based on the value existing in the document. So if its true, change it to false and if its false, change it to true. Example array.

[{ _id: 59cb78434436be173e038aaa, active: true, title: 'One' },
{ _id: 59cb78434436be173e038aab, active: false, title: 'Two' },
{ _id: 59cb78434436be173e038aac, active: false, title: 'Three' }]

const todos = db.collection('todos');

const active = !active;
await todos.findOneAndUpdate({ _id: ObjectID(args.id) },
{ $set: { active: active } }, function(err, doc) {
                if (err) {
                    throw err;
                } else {
                    console.log('Updated');
                }
            });

I cant set this directly by passing true or false to active { $set: { active: true }}. How would I test the value and return the opposite?

Thanks


Solution

  • At the moment there is no $toggle operator in MongoDB, so it is impossible to make such switch operation atomic. But there is some kind of workaround for this functionality. First of all you need to replace your boolean with the number. Then instead of trying to replace it with the opposite value you should just increment it every time.

    todos.findOneAndUpdate({_id: ObjectId(args.id)}, {$inc:{ active: 1}});
    

    So you see that everytime it will be $incremented by 1 and it means it will always switch from even number to odd.

    The next step is to modify your 'get' query in this way:

    todos.find({'active' : { '$mod' : [ 2, 1 ] });
    

    It will return all documents where 'active' field is now odd number, which you can consider like true for example, or vice versa.