I have a case where I want to run a query that MUST match some field conditions but SHOULD match some others. If they don’t match however the query should still return the conditions that MUST.
For example let’s say in a collection I have 3 documents such as:
{ _id: 1, position: “Developer”, name: “Greg”, surname: “Smith” },
{ _id: 2, position: “QA”, name: “Andrew”, surname: “Samson” },
{ _id: 3, position: “Developer”, name: “Adam”, surname: “Mount” }
If I run a query with a condition { position: “Developer” } that is a MUST and { name: “Greg” } that is a SHOULD I should just get the record:
{ _id: 1, position: “Developer”, name: “Greg”, surname: “Smith” }
However, if I run the query again with { position: “Developer” } and { “name”: “Daniel” }, I should get all records that match the MUST condition. So return records:
{ _id: 1, position: “Developer”, name: “Greg”, surname: “Smith” },
{ _id: 3, position: “Developer”, name: “Adam”, surname: “Mount” }
Furthermore, if I have a query with MUST conditon { position: “Developer” } and SHOULD conditions { name: “Greg”, surname: “Garbrandt” } I should still get:
{ _id: 1, position: “Developer”, name: “Greg”, surname: “Smith” }
Not sure if there is a way to write the query to work like this or if there is a functionality that could do this outright.