I have following documents in Agriculture model
{
"_id" : ObjectId("5e5c9c0a0cfcdb1538406000"),
"agricultureProductSalesType" : [
"cash_crops",
"vegetable",
"fruit"
],
"flag" : false,
"agricultureDetail" : [
{
"production" : {
"plantCount" : 0,
"kg" : 0,
"muri" : 0,
"pathi" : 0
},
"sale" : {
"plantCount" : 0,
"kg" : 0,
"muri" : 0,
"pathi" : 0
},
"_id" : ObjectId("5e5c9c0a0cfcdb1538406001"),
"title" : "alaichi",
"agricultureProductionSalesType" : "cash_crops"
},
{
"production" : {
"plantCount" : 0,
"kg" : 40,
"muri" : 0,
"pathi" : 0
},
"sale" : {
"plantCount" : 0,
"kg" : 0,
"muri" : 0,
"pathi" : 0
},
"_id" : ObjectId("5e5c9c0a0cfcdb1538406002"),
"title" : "amriso",
"agricultureProductionSalesType" : "cash_crops"
}
],
"agricultureParent" : [
{
"area" : {
"ropani" : 10,
"aana" : 0,
"paisa" : 0
},
"_id" : ObjectId("5e5c9c0a0cfcdb1538406005"),
"title" : "cash_crops",
"income" : 50000,
"expense" : 6000
}
],
"house" : ObjectId("5e5c9c090cfcdb1538405fa9"),
"agricultureProductSales" : true,
"insecticides" : false,
"fertilizerUse" : true,
"seedNeed" : "local",
"__v" : 0
I want result from above document with condition if agricultureDetail.title is not empty or blank string AND agricultureDetail.production.plantcount equals zero or null or exists false AND agricultureDetail.production.kg equals zero or null or exists false AND (all remaining elements inside production zero or null or exists flase).
I tried $elemMatch as bellow:
$and: [
{ agricultureProductSales: true },
{ agricultureDetail: { $exists: true, $ne: [] } },
{
$or: [
{ agricultureDetail: { $elemMatch: { title: { $ne: "" } } } },
{
agricultureDetail: { $elemMatch: { title: { $exists: true } } }
}
]
},
{
$or: [
{
agricultureDetail: {
$elemMatch: { production: { $exists: true } }
}
},
{
agricultureDetail: {
$elemMatch: { production: { $ne: [] } }
}
}
]
},
{
$or: [
{
"agricultureDetail.production": {
$elemMatch: { plantCount: { $exists: false } }
}
},
{
"agricultureDetail.production": {
$elemMatch: { plantCount: { $eq: 0 } }
}
},
{
"agricultureDetail.production": {
$elemMatch: { plantCount: { $eq: null } }
}
}
]
}
]
But it reurns empty result. Any help? THankyou so much.
{"agricultureDetail.production": { $elemMatch: { plantCount: { $eq: null } } }is equivalent to{"agricultureDetail.production.plantCount": { $eq: null } }$ormatching$exists:trueor$ne:[]will always match.$ne:[]matches documents for which the field does not exist, so that is essentially testing{$exists:true}or{$exists:false}likewise with the$ne: ""or$exists: true