I am trying to build a mongo query (below) but not getting any results back, my Mongo collection fields (shown in picture). I am trying to elemMatch on Packaes.Tier = Gold, so expecting to get back the Packages array with one element for the Gold Tier.
db.getCollection('FacilityProfiles').find({$and : [
{"Packages" : {$elemMatch : {"Packages.Tier" : "Gold"}}}
]},
{"Packages" : 1}
)
I also tried these queries but it returned both elements:
db.getCollection('FacilityProfiles').find({$and : [
{"Packages.StartDate" : {$lte : new Date}}
]},
{"Packages" : 1}
)
db.getCollection('FacilityProfiles').find({$and : [
{"Packages.StartDate" : {$lte : new Date}},
{"Packages.EndDate" : {$gte : new Date}}
]},
{"Packages" : 1}
)
db.getCollection('FacilityProfiles').find({$and : [
{"ProfileLanguage" : "EN"},{"spctr_FacilityId" : "000018211"},
{"Packages.StartDate" : {$lte : new Date},"Packages.EndDate" : {$gte : new Date}}
]},
{"Packages" : 1}
)
So based on my Mongo collection values is possible to build a query to get only the gold package element from the Packages array I am trying to get only the Package element where the current date is between start and end date essentially currentdate between StartDate and EndDate
UPdate: I tried $elemMatch but still get back both elements in the array
db.getCollection('FacilityProfiles').find(
{"Packages" : { $elemMatch : { "PackageCode" : "G", "StartDate" : {$lte : new Date()}, "EndDate" : {$gte : new Date()} }}}
)