Find documents in database by a date (year);





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying to make a query to my database, i have a collection with some documents that contains a field type Date, i would like to find all documents where the Date is equal to the year that i choose, what is the best way to do it?



At this moment i am receiving the documents like this.



facturasCtrl.filterDates = async (req, res) => {

facturas = await facturasModel.find({ date: { '$gte': req.body.date1, '$lt': req.body.date2} })
res.json(facturas);
}


//date1 = 1.1.2018
//date2 = 31.12.2018



but i think this way is not correct at all... there is any method to find all documents from a date?










share|improve this question

























  • Nothing wrong with the way you're doing it.

    – JohnnyHK
    Nov 26 '18 at 16:51











  • hehe, yeah actually it is working... but do you know if there is any method that... for example... if i send a year from my client , the database find all documents in that year? @JohnnyHK

    – Sergio Cano
    Nov 26 '18 at 17:03


















1















I am trying to make a query to my database, i have a collection with some documents that contains a field type Date, i would like to find all documents where the Date is equal to the year that i choose, what is the best way to do it?



At this moment i am receiving the documents like this.



facturasCtrl.filterDates = async (req, res) => {

facturas = await facturasModel.find({ date: { '$gte': req.body.date1, '$lt': req.body.date2} })
res.json(facturas);
}


//date1 = 1.1.2018
//date2 = 31.12.2018



but i think this way is not correct at all... there is any method to find all documents from a date?










share|improve this question

























  • Nothing wrong with the way you're doing it.

    – JohnnyHK
    Nov 26 '18 at 16:51











  • hehe, yeah actually it is working... but do you know if there is any method that... for example... if i send a year from my client , the database find all documents in that year? @JohnnyHK

    – Sergio Cano
    Nov 26 '18 at 17:03














1












1








1








I am trying to make a query to my database, i have a collection with some documents that contains a field type Date, i would like to find all documents where the Date is equal to the year that i choose, what is the best way to do it?



At this moment i am receiving the documents like this.



facturasCtrl.filterDates = async (req, res) => {

facturas = await facturasModel.find({ date: { '$gte': req.body.date1, '$lt': req.body.date2} })
res.json(facturas);
}


//date1 = 1.1.2018
//date2 = 31.12.2018



but i think this way is not correct at all... there is any method to find all documents from a date?










share|improve this question
















I am trying to make a query to my database, i have a collection with some documents that contains a field type Date, i would like to find all documents where the Date is equal to the year that i choose, what is the best way to do it?



At this moment i am receiving the documents like this.



facturasCtrl.filterDates = async (req, res) => {

facturas = await facturasModel.find({ date: { '$gte': req.body.date1, '$lt': req.body.date2} })
res.json(facturas);
}


//date1 = 1.1.2018
//date2 = 31.12.2018



but i think this way is not correct at all... there is any method to find all documents from a date?







mongodb mongodb-query






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 18:01









Anthony Winzlet

18.3k42346




18.3k42346










asked Nov 26 '18 at 16:48









Sergio CanoSergio Cano

7310




7310













  • Nothing wrong with the way you're doing it.

    – JohnnyHK
    Nov 26 '18 at 16:51











  • hehe, yeah actually it is working... but do you know if there is any method that... for example... if i send a year from my client , the database find all documents in that year? @JohnnyHK

    – Sergio Cano
    Nov 26 '18 at 17:03



















  • Nothing wrong with the way you're doing it.

    – JohnnyHK
    Nov 26 '18 at 16:51











  • hehe, yeah actually it is working... but do you know if there is any method that... for example... if i send a year from my client , the database find all documents in that year? @JohnnyHK

    – Sergio Cano
    Nov 26 '18 at 17:03

















Nothing wrong with the way you're doing it.

– JohnnyHK
Nov 26 '18 at 16:51





Nothing wrong with the way you're doing it.

– JohnnyHK
Nov 26 '18 at 16:51













hehe, yeah actually it is working... but do you know if there is any method that... for example... if i send a year from my client , the database find all documents in that year? @JohnnyHK

– Sergio Cano
Nov 26 '18 at 17:03





hehe, yeah actually it is working... but do you know if there is any method that... for example... if i send a year from my client , the database find all documents in that year? @JohnnyHK

– Sergio Cano
Nov 26 '18 at 17:03












1 Answer
1






active

oldest

votes


















1














You can use $expr with the $year aggregation operator



const year = 2018  

db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })





share|improve this answer
























  • i am tryng this way, but something is not working, i receive a JSON object from my client like this. { year: 2018 } And then i do this. facturasCtrl.filterYear = async (req, res) => { console.log(req.body) facturas = await facturasModel.find({ "$expr": { "$eq": [{ "$year": "$date" }, req.body.year] } }) res.json(facturas); } I do receive the documents, but if change the year, and send 2019... i am still receiving from 2018

    – Sergio Cano
    Nov 26 '18 at 17:23













  • Are you getting any error? What is the type the date in your database. Is it of type String or Date?

    – Anthony Winzlet
    Nov 26 '18 at 17:25











  • in my database is type Date, but i dont get any error

    – Sergio Cano
    Nov 26 '18 at 17:42













  • Then it should work. What is the type of the year passed from the font end? Is it string of number?

    – Anthony Winzlet
    Nov 26 '18 at 17:50








  • 1





    ohh, thank you!, really usefull website, it seems working in the web... so i have to check all. thank you again!

    – Sergio Cano
    Nov 26 '18 at 17:55












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53485613%2ffind-documents-in-database-by-a-date-year%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can use $expr with the $year aggregation operator



const year = 2018  

db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })





share|improve this answer
























  • i am tryng this way, but something is not working, i receive a JSON object from my client like this. { year: 2018 } And then i do this. facturasCtrl.filterYear = async (req, res) => { console.log(req.body) facturas = await facturasModel.find({ "$expr": { "$eq": [{ "$year": "$date" }, req.body.year] } }) res.json(facturas); } I do receive the documents, but if change the year, and send 2019... i am still receiving from 2018

    – Sergio Cano
    Nov 26 '18 at 17:23













  • Are you getting any error? What is the type the date in your database. Is it of type String or Date?

    – Anthony Winzlet
    Nov 26 '18 at 17:25











  • in my database is type Date, but i dont get any error

    – Sergio Cano
    Nov 26 '18 at 17:42













  • Then it should work. What is the type of the year passed from the font end? Is it string of number?

    – Anthony Winzlet
    Nov 26 '18 at 17:50








  • 1





    ohh, thank you!, really usefull website, it seems working in the web... so i have to check all. thank you again!

    – Sergio Cano
    Nov 26 '18 at 17:55
















1














You can use $expr with the $year aggregation operator



const year = 2018  

db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })





share|improve this answer
























  • i am tryng this way, but something is not working, i receive a JSON object from my client like this. { year: 2018 } And then i do this. facturasCtrl.filterYear = async (req, res) => { console.log(req.body) facturas = await facturasModel.find({ "$expr": { "$eq": [{ "$year": "$date" }, req.body.year] } }) res.json(facturas); } I do receive the documents, but if change the year, and send 2019... i am still receiving from 2018

    – Sergio Cano
    Nov 26 '18 at 17:23













  • Are you getting any error? What is the type the date in your database. Is it of type String or Date?

    – Anthony Winzlet
    Nov 26 '18 at 17:25











  • in my database is type Date, but i dont get any error

    – Sergio Cano
    Nov 26 '18 at 17:42













  • Then it should work. What is the type of the year passed from the font end? Is it string of number?

    – Anthony Winzlet
    Nov 26 '18 at 17:50








  • 1





    ohh, thank you!, really usefull website, it seems working in the web... so i have to check all. thank you again!

    – Sergio Cano
    Nov 26 '18 at 17:55














1












1








1







You can use $expr with the $year aggregation operator



const year = 2018  

db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })





share|improve this answer













You can use $expr with the $year aggregation operator



const year = 2018  

db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 17:05









Anthony WinzletAnthony Winzlet

18.3k42346




18.3k42346













  • i am tryng this way, but something is not working, i receive a JSON object from my client like this. { year: 2018 } And then i do this. facturasCtrl.filterYear = async (req, res) => { console.log(req.body) facturas = await facturasModel.find({ "$expr": { "$eq": [{ "$year": "$date" }, req.body.year] } }) res.json(facturas); } I do receive the documents, but if change the year, and send 2019... i am still receiving from 2018

    – Sergio Cano
    Nov 26 '18 at 17:23













  • Are you getting any error? What is the type the date in your database. Is it of type String or Date?

    – Anthony Winzlet
    Nov 26 '18 at 17:25











  • in my database is type Date, but i dont get any error

    – Sergio Cano
    Nov 26 '18 at 17:42













  • Then it should work. What is the type of the year passed from the font end? Is it string of number?

    – Anthony Winzlet
    Nov 26 '18 at 17:50








  • 1





    ohh, thank you!, really usefull website, it seems working in the web... so i have to check all. thank you again!

    – Sergio Cano
    Nov 26 '18 at 17:55



















  • i am tryng this way, but something is not working, i receive a JSON object from my client like this. { year: 2018 } And then i do this. facturasCtrl.filterYear = async (req, res) => { console.log(req.body) facturas = await facturasModel.find({ "$expr": { "$eq": [{ "$year": "$date" }, req.body.year] } }) res.json(facturas); } I do receive the documents, but if change the year, and send 2019... i am still receiving from 2018

    – Sergio Cano
    Nov 26 '18 at 17:23













  • Are you getting any error? What is the type the date in your database. Is it of type String or Date?

    – Anthony Winzlet
    Nov 26 '18 at 17:25











  • in my database is type Date, but i dont get any error

    – Sergio Cano
    Nov 26 '18 at 17:42













  • Then it should work. What is the type of the year passed from the font end? Is it string of number?

    – Anthony Winzlet
    Nov 26 '18 at 17:50








  • 1





    ohh, thank you!, really usefull website, it seems working in the web... so i have to check all. thank you again!

    – Sergio Cano
    Nov 26 '18 at 17:55

















i am tryng this way, but something is not working, i receive a JSON object from my client like this. { year: 2018 } And then i do this. facturasCtrl.filterYear = async (req, res) => { console.log(req.body) facturas = await facturasModel.find({ "$expr": { "$eq": [{ "$year": "$date" }, req.body.year] } }) res.json(facturas); } I do receive the documents, but if change the year, and send 2019... i am still receiving from 2018

– Sergio Cano
Nov 26 '18 at 17:23







i am tryng this way, but something is not working, i receive a JSON object from my client like this. { year: 2018 } And then i do this. facturasCtrl.filterYear = async (req, res) => { console.log(req.body) facturas = await facturasModel.find({ "$expr": { "$eq": [{ "$year": "$date" }, req.body.year] } }) res.json(facturas); } I do receive the documents, but if change the year, and send 2019... i am still receiving from 2018

– Sergio Cano
Nov 26 '18 at 17:23















Are you getting any error? What is the type the date in your database. Is it of type String or Date?

– Anthony Winzlet
Nov 26 '18 at 17:25





Are you getting any error? What is the type the date in your database. Is it of type String or Date?

– Anthony Winzlet
Nov 26 '18 at 17:25













in my database is type Date, but i dont get any error

– Sergio Cano
Nov 26 '18 at 17:42







in my database is type Date, but i dont get any error

– Sergio Cano
Nov 26 '18 at 17:42















Then it should work. What is the type of the year passed from the font end? Is it string of number?

– Anthony Winzlet
Nov 26 '18 at 17:50







Then it should work. What is the type of the year passed from the font end? Is it string of number?

– Anthony Winzlet
Nov 26 '18 at 17:50






1




1





ohh, thank you!, really usefull website, it seems working in the web... so i have to check all. thank you again!

– Sergio Cano
Nov 26 '18 at 17:55





ohh, thank you!, really usefull website, it seems working in the web... so i have to check all. thank you again!

– Sergio Cano
Nov 26 '18 at 17:55




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53485613%2ffind-documents-in-database-by-a-date-year%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft