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;
}
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
add a comment |
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
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
add a comment |
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
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
mongodb mongodb-query
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
You can use $expr with the $year aggregation operator
const year = 2018
db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })
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 typeStringorDate?
– 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 itstringofnumber?
– 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
|
show 2 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
You can use $expr with the $year aggregation operator
const year = 2018
db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })
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 typeStringorDate?
– 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 itstringofnumber?
– 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
|
show 2 more comments
You can use $expr with the $year aggregation operator
const year = 2018
db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })
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 typeStringorDate?
– 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 itstringofnumber?
– 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
|
show 2 more comments
You can use $expr with the $year aggregation operator
const year = 2018
db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })
You can use $expr with the $year aggregation operator
const year = 2018
db.collection.find({ "$expr": { "$eq": [{ "$year": "$date" }, year] } })
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 typeStringorDate?
– 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 itstringofnumber?
– 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
|
show 2 more comments
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 typeStringorDate?
– 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 itstringofnumber?
– 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
|
show 2 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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