I have a mongo collection with multiple configurations. However, at any one time, only one of these can be "active". I do this by setting an active
tag to true.
CylonConfigurations = new Mongo.Collection('cylon_configurations');
Meteor.startup(function () {
if (Meteor.isServer) {
CylonConfigurations.upsert({
host: '192.168.1.4',
port: 23
}, {
$setOnInsert: {
host: '192.168.1.4',
port: 23,
active: true
}
}, function (err, s) {
if (!err) {
const conn = CylonConfigurations.findOne({ active: true });
Cylon.connect(conn.port, conn.host);
}
});
}
});
However, the problem I am facing is the case that multiple items within this collection can have the boolean of active
set to true
, theoretically.
In MongoDB, is there any way to prevent this? Is there any way to ensure that only one document in a collection has a flag set to true, and all the others have it set to false?
true
and only one, is to set "everything" tofalse
and then one totrue
. In the case of "upserts", both of those actions would take place "after" the upsert has occurred. – Wisp