Answer the question
In order to leave comments, you need to log in
Is it possible to skip null?
Here is the model
const schema = new Schema({
// ....
conditions: {},
// ....
});
{
"conditions": {
"age": 10,
"name": "John"
}
}
const conditions = {
'conditions.age': 10,
'conditions.name': 'John',
'conditions.surname': 'White' // surname там нет
}
const result = await Model.find(conditions);
console.log(result) // [];
Answer the question
In order to leave comments, you need to log in
Decision
const validConditions = {
$and: [
{
$or: [
{ 'conditions.age': { $exists: false } },
{ 'conditions.age': 20 },
],
},
{
$or: [
{ 'conditions.name': { $exists: false } },
{ 'conditions.name': 'John' },
],
},
{
$or: [
{ 'conditions.surname': { $exists: false } },
{ 'conditions.surname': 'White' },
],
},
],
};
const result = await Test.find(validConditions);
SELECT * FROM entries WHERE age = 20 OR age IS NULL AND name = 'John' OR name IS NULL AND surname = 'White' OR surname IS NULL
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question