MongoDb Query currentdate is between two data field values in Mongo [duplicate]












0
















This question already has an answer here:




  • find in array between dates in mongo

    2 answers



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers




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



my mongo collection is:
enter image description here



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()} }}}
)









share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 20:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You actually started with the correct operator and then it all went wrong. Read the $elemMatch documentation again and also reference Specify Multiple Conditions for Array of Documents in the documentation as well. Both actually give the same example of a "between" for an array element.

    – Neil Lunn
    Nov 22 '18 at 20:15











  • Thanks for your suggestions. I did try with $elemMatch but i still get both elements in the array returned: db.getCollection('FacilityProfiles').find( {"Packages" : { $elemMatch : { "PackageCode" : "G", "StartDate" : {$lte : new Date()}, "EndDate" : {$gte : new Date()} }}} )

    – Jawahar
    Nov 22 '18 at 21:24













  • That's actually expected. "Filtering Arrays" is a different thing to "Querying a Document". It also has of course an existing answer and is a very common question.

    – Neil Lunn
    Nov 22 '18 at 21:27
















0
















This question already has an answer here:




  • find in array between dates in mongo

    2 answers



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers




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



my mongo collection is:
enter image description here



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()} }}}
)









share|improve this question















marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 20:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You actually started with the correct operator and then it all went wrong. Read the $elemMatch documentation again and also reference Specify Multiple Conditions for Array of Documents in the documentation as well. Both actually give the same example of a "between" for an array element.

    – Neil Lunn
    Nov 22 '18 at 20:15











  • Thanks for your suggestions. I did try with $elemMatch but i still get both elements in the array returned: db.getCollection('FacilityProfiles').find( {"Packages" : { $elemMatch : { "PackageCode" : "G", "StartDate" : {$lte : new Date()}, "EndDate" : {$gte : new Date()} }}} )

    – Jawahar
    Nov 22 '18 at 21:24













  • That's actually expected. "Filtering Arrays" is a different thing to "Querying a Document". It also has of course an existing answer and is a very common question.

    – Neil Lunn
    Nov 22 '18 at 21:27














0












0








0









This question already has an answer here:




  • find in array between dates in mongo

    2 answers



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers




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



my mongo collection is:
enter image description here



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()} }}}
)









share|improve this question

















This question already has an answer here:




  • find in array between dates in mongo

    2 answers



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers




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



my mongo collection is:
enter image description here



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()} }}}
)




This question already has an answer here:




  • find in array between dates in mongo

    2 answers



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers








mongodb between






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 21:29







Jawahar

















asked Nov 22 '18 at 18:49









JawaharJawahar

28110




28110




marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 20:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 20:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You actually started with the correct operator and then it all went wrong. Read the $elemMatch documentation again and also reference Specify Multiple Conditions for Array of Documents in the documentation as well. Both actually give the same example of a "between" for an array element.

    – Neil Lunn
    Nov 22 '18 at 20:15











  • Thanks for your suggestions. I did try with $elemMatch but i still get both elements in the array returned: db.getCollection('FacilityProfiles').find( {"Packages" : { $elemMatch : { "PackageCode" : "G", "StartDate" : {$lte : new Date()}, "EndDate" : {$gte : new Date()} }}} )

    – Jawahar
    Nov 22 '18 at 21:24













  • That's actually expected. "Filtering Arrays" is a different thing to "Querying a Document". It also has of course an existing answer and is a very common question.

    – Neil Lunn
    Nov 22 '18 at 21:27



















  • You actually started with the correct operator and then it all went wrong. Read the $elemMatch documentation again and also reference Specify Multiple Conditions for Array of Documents in the documentation as well. Both actually give the same example of a "between" for an array element.

    – Neil Lunn
    Nov 22 '18 at 20:15











  • Thanks for your suggestions. I did try with $elemMatch but i still get both elements in the array returned: db.getCollection('FacilityProfiles').find( {"Packages" : { $elemMatch : { "PackageCode" : "G", "StartDate" : {$lte : new Date()}, "EndDate" : {$gte : new Date()} }}} )

    – Jawahar
    Nov 22 '18 at 21:24













  • That's actually expected. "Filtering Arrays" is a different thing to "Querying a Document". It also has of course an existing answer and is a very common question.

    – Neil Lunn
    Nov 22 '18 at 21:27

















You actually started with the correct operator and then it all went wrong. Read the $elemMatch documentation again and also reference Specify Multiple Conditions for Array of Documents in the documentation as well. Both actually give the same example of a "between" for an array element.

– Neil Lunn
Nov 22 '18 at 20:15





You actually started with the correct operator and then it all went wrong. Read the $elemMatch documentation again and also reference Specify Multiple Conditions for Array of Documents in the documentation as well. Both actually give the same example of a "between" for an array element.

– Neil Lunn
Nov 22 '18 at 20:15













Thanks for your suggestions. I did try with $elemMatch but i still get both elements in the array returned: db.getCollection('FacilityProfiles').find( {"Packages" : { $elemMatch : { "PackageCode" : "G", "StartDate" : {$lte : new Date()}, "EndDate" : {$gte : new Date()} }}} )

– Jawahar
Nov 22 '18 at 21:24







Thanks for your suggestions. I did try with $elemMatch but i still get both elements in the array returned: db.getCollection('FacilityProfiles').find( {"Packages" : { $elemMatch : { "PackageCode" : "G", "StartDate" : {$lte : new Date()}, "EndDate" : {$gte : new Date()} }}} )

– Jawahar
Nov 22 '18 at 21:24















That's actually expected. "Filtering Arrays" is a different thing to "Querying a Document". It also has of course an existing answer and is a very common question.

– Neil Lunn
Nov 22 '18 at 21:27





That's actually expected. "Filtering Arrays" is a different thing to "Querying a Document". It also has of course an existing answer and is a very common question.

– Neil Lunn
Nov 22 '18 at 21:27












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python