'How to filter documents by having a list of words which should not be inside of them in MongoDB Atlas Search?
For example I have the array ["java", "perl", "scrum"]
and the following documents:
{
id: 1,
title: "Java Software Developer"
},
{
id: 2,
title: "Senior Software Engineer"
},
{
id: 3,
title: "Perl developer familiar with SCRUM methodology"
}
What I'd like to do in an atlas search aggregation pipeline is that to filter out the documents in which the title contains one of the words in the array. I've tried negating a $phrase, and also using Regex, but neither worked. Is there an elegant way to handle this situation, and if yes what would it be?
EDIT: After the aggregation only document with id 2 would be returned.
Solution 1:[1]
You could use the $nin
(not in) operator with some regex inside. However, this type of query will not be very efficient.
db.collection.find({
title: { "$nin": [/java/i, /perl/i, /scrum/i]}
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rubén Vega |