Answer the question
In order to leave comments, you need to log in
How to correctly highlight a specific element in MongoDB?
As it was before:
There was a list of elements that have a boolean active field. Only one element can be active at a time, so when we do active = true for some element, then before that we will make active = false for everyone.
But now MongoDB is being used and something tells me that everything can be done wrong there. The idea comes to mind to make a collection, where there will be only one single document containing the id of the active element.
And in general, if you need to somehow select a group of elements, is it better for them to make a boolean field indicating membership in a group, or to make a collection of id-shnikov?
Answer the question
In order to leave comments, you need to log in
In principle, before making a document from the collection active, you can first search the collection for an already existing document for which active: true and change it to false. And after that make the desired one active.
With monggose it would look something like this:
var idToUpdate = 1;
mongoModel.findOneAndUpdate({active: true}, {active: false}, function(err, result){
if(!err){
mongoModel.update({_id: idToUpdate }, {active: true}, function(err, result){
console.log('Updated')
})
}
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question