Couch DB query to fetch the records greater than a particular date
I am trying to fetch the records that has the "effectiveDateOfAction" field greater than Oct'2017. Please find the below 3 records.
{
"_id": "TRAN001",
"_rev": "13-59a53069c1ebd6ecfc23ca1dea0ba28f",
"effectiveDateOfAction": "10-30-2018",
"employeeName": "Kumar,Vinoth",
"transferReportID": "TRAN001",
"~version": "76:0"
}
{
"_id": "TRAN001",
"_rev": "12-c320c61168f5d6d020f971124cb395f2",
"effectiveDateOfAction": "05-10-2018",
"employeeName": "Vinoth",
"transferReportID": "TRAN002",
"~version": "77:0"
}
{
"_id": "TRAN003",
"_rev": "16-567a15e9ea7e2349d4c24816e7eafda3",
"effectiveDateOfAction": "10-20-2017",
"employeeName": "Kumar",
"transferReportID": "TRAN003",
"~version": "78:0"
}
Please find my query below which i tried.I am checking using Project Fauxton.
{"selector": {"$and": [{"transferReportID": {"$ne": null}},{"effectiveDateOfAction": {"$gt": "10-31-2017"}}]}}
Please help me getting the correct query.
couchdb couchdb-mango fauxton
add a comment |
I am trying to fetch the records that has the "effectiveDateOfAction" field greater than Oct'2017. Please find the below 3 records.
{
"_id": "TRAN001",
"_rev": "13-59a53069c1ebd6ecfc23ca1dea0ba28f",
"effectiveDateOfAction": "10-30-2018",
"employeeName": "Kumar,Vinoth",
"transferReportID": "TRAN001",
"~version": "76:0"
}
{
"_id": "TRAN001",
"_rev": "12-c320c61168f5d6d020f971124cb395f2",
"effectiveDateOfAction": "05-10-2018",
"employeeName": "Vinoth",
"transferReportID": "TRAN002",
"~version": "77:0"
}
{
"_id": "TRAN003",
"_rev": "16-567a15e9ea7e2349d4c24816e7eafda3",
"effectiveDateOfAction": "10-20-2017",
"employeeName": "Kumar",
"transferReportID": "TRAN003",
"~version": "78:0"
}
Please find my query below which i tried.I am checking using Project Fauxton.
{"selector": {"$and": [{"transferReportID": {"$ne": null}},{"effectiveDateOfAction": {"$gt": "10-31-2017"}}]}}
Please help me getting the correct query.
couchdb couchdb-mango fauxton
"$gt": "10-31-2017"
is a lexicographic string comparison, so10-31-2018
will be greater than01-01-2019
. You may have to create a view, which emits your data sorted by date (see docs).
– Enno
Nov 20 at 9:40
@Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
– Priyanka
Nov 20 at 11:33
add a comment |
I am trying to fetch the records that has the "effectiveDateOfAction" field greater than Oct'2017. Please find the below 3 records.
{
"_id": "TRAN001",
"_rev": "13-59a53069c1ebd6ecfc23ca1dea0ba28f",
"effectiveDateOfAction": "10-30-2018",
"employeeName": "Kumar,Vinoth",
"transferReportID": "TRAN001",
"~version": "76:0"
}
{
"_id": "TRAN001",
"_rev": "12-c320c61168f5d6d020f971124cb395f2",
"effectiveDateOfAction": "05-10-2018",
"employeeName": "Vinoth",
"transferReportID": "TRAN002",
"~version": "77:0"
}
{
"_id": "TRAN003",
"_rev": "16-567a15e9ea7e2349d4c24816e7eafda3",
"effectiveDateOfAction": "10-20-2017",
"employeeName": "Kumar",
"transferReportID": "TRAN003",
"~version": "78:0"
}
Please find my query below which i tried.I am checking using Project Fauxton.
{"selector": {"$and": [{"transferReportID": {"$ne": null}},{"effectiveDateOfAction": {"$gt": "10-31-2017"}}]}}
Please help me getting the correct query.
couchdb couchdb-mango fauxton
I am trying to fetch the records that has the "effectiveDateOfAction" field greater than Oct'2017. Please find the below 3 records.
{
"_id": "TRAN001",
"_rev": "13-59a53069c1ebd6ecfc23ca1dea0ba28f",
"effectiveDateOfAction": "10-30-2018",
"employeeName": "Kumar,Vinoth",
"transferReportID": "TRAN001",
"~version": "76:0"
}
{
"_id": "TRAN001",
"_rev": "12-c320c61168f5d6d020f971124cb395f2",
"effectiveDateOfAction": "05-10-2018",
"employeeName": "Vinoth",
"transferReportID": "TRAN002",
"~version": "77:0"
}
{
"_id": "TRAN003",
"_rev": "16-567a15e9ea7e2349d4c24816e7eafda3",
"effectiveDateOfAction": "10-20-2017",
"employeeName": "Kumar",
"transferReportID": "TRAN003",
"~version": "78:0"
}
Please find my query below which i tried.I am checking using Project Fauxton.
{"selector": {"$and": [{"transferReportID": {"$ne": null}},{"effectiveDateOfAction": {"$gt": "10-31-2017"}}]}}
Please help me getting the correct query.
couchdb couchdb-mango fauxton
couchdb couchdb-mango fauxton
edited Nov 20 at 7:25
Neil Lunn
96.8k22170180
96.8k22170180
asked Nov 20 at 7:24
Priyanka
87
87
"$gt": "10-31-2017"
is a lexicographic string comparison, so10-31-2018
will be greater than01-01-2019
. You may have to create a view, which emits your data sorted by date (see docs).
– Enno
Nov 20 at 9:40
@Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
– Priyanka
Nov 20 at 11:33
add a comment |
"$gt": "10-31-2017"
is a lexicographic string comparison, so10-31-2018
will be greater than01-01-2019
. You may have to create a view, which emits your data sorted by date (see docs).
– Enno
Nov 20 at 9:40
@Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
– Priyanka
Nov 20 at 11:33
"$gt": "10-31-2017"
is a lexicographic string comparison, so 10-31-2018
will be greater than 01-01-2019
. You may have to create a view, which emits your data sorted by date (see docs).– Enno
Nov 20 at 9:40
"$gt": "10-31-2017"
is a lexicographic string comparison, so 10-31-2018
will be greater than 01-01-2019
. You may have to create a view, which emits your data sorted by date (see docs).– Enno
Nov 20 at 9:40
@Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
– Priyanka
Nov 20 at 11:33
@Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
– Priyanka
Nov 20 at 11:33
add a comment |
2 Answers
2
active
oldest
votes
As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.
I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.
You can then use a query using the "$gte" operator:
{
"selector": {
"effectiveDateOfAction": {
"$gte": "2018-10-01"
}
}
}
This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.
See this blog post on how to store and query dates in CouchDB.
add a comment |
If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).
If you can't change your data, you can create a view, which transforms your dates into a sortable format.
Example: Put a design doc similar to the following to your database
{
_id: '_design/employees',
views: {
by_action_date: {
map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
}
}
}
The map
function has to be given as string in the document, formatted it is:
function(doc) {
if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
}
}
You can then query it to get your employees sorted:
Use include_docs = true
param to get your real documents included.
/my-database/_design/employees/_view/by_action_date?include_docs=true
You then can also use startkey
and endkey
params, to limit to a specific time frame:
/my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"
This will return your TRAN001
and TRAN002
documents.
add a comment |
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%2f53388100%2fcouch-db-query-to-fetch-the-records-greater-than-a-particular-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.
I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.
You can then use a query using the "$gte" operator:
{
"selector": {
"effectiveDateOfAction": {
"$gte": "2018-10-01"
}
}
}
This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.
See this blog post on how to store and query dates in CouchDB.
add a comment |
As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.
I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.
You can then use a query using the "$gte" operator:
{
"selector": {
"effectiveDateOfAction": {
"$gte": "2018-10-01"
}
}
}
This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.
See this blog post on how to store and query dates in CouchDB.
add a comment |
As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.
I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.
You can then use a query using the "$gte" operator:
{
"selector": {
"effectiveDateOfAction": {
"$gte": "2018-10-01"
}
}
}
This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.
See this blog post on how to store and query dates in CouchDB.
As there is no native date type in JSON, it's important to store dates in a format that makes sense at query time. The "Month-Day-Year" format may be useful when rendering dates for a US audience but it makes little sense for querying.
I would suggest the "YYYY-MM-DD" format e.g "2018-10-30". This stores the same data as before but the sort order happens to be in date order, because years are longer than months and months are longer than days.
You can then use a query using the "$gte" operator:
{
"selector": {
"effectiveDateOfAction": {
"$gte": "2018-10-01"
}
}
}
This reads as "fetch documents whose 'effectiveDateOfAction' field is greater than or equal to 1st October 2018'.
See this blog post on how to store and query dates in CouchDB.
answered Nov 23 at 11:00
Glynn Bird
3,7451817
3,7451817
add a comment |
add a comment |
If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).
If you can't change your data, you can create a view, which transforms your dates into a sortable format.
Example: Put a design doc similar to the following to your database
{
_id: '_design/employees',
views: {
by_action_date: {
map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
}
}
}
The map
function has to be given as string in the document, formatted it is:
function(doc) {
if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
}
}
You can then query it to get your employees sorted:
Use include_docs = true
param to get your real documents included.
/my-database/_design/employees/_view/by_action_date?include_docs=true
You then can also use startkey
and endkey
params, to limit to a specific time frame:
/my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"
This will return your TRAN001
and TRAN002
documents.
add a comment |
If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).
If you can't change your data, you can create a view, which transforms your dates into a sortable format.
Example: Put a design doc similar to the following to your database
{
_id: '_design/employees',
views: {
by_action_date: {
map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
}
}
}
The map
function has to be given as string in the document, formatted it is:
function(doc) {
if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
}
}
You can then query it to get your employees sorted:
Use include_docs = true
param to get your real documents included.
/my-database/_design/employees/_view/by_action_date?include_docs=true
You then can also use startkey
and endkey
params, to limit to a specific time frame:
/my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"
This will return your TRAN001
and TRAN002
documents.
add a comment |
If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).
If you can't change your data, you can create a view, which transforms your dates into a sortable format.
Example: Put a design doc similar to the following to your database
{
_id: '_design/employees',
views: {
by_action_date: {
map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
}
}
}
The map
function has to be given as string in the document, formatted it is:
function(doc) {
if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
}
}
You can then query it to get your employees sorted:
Use include_docs = true
param to get your real documents included.
/my-database/_design/employees/_view/by_action_date?include_docs=true
You then can also use startkey
and endkey
params, to limit to a specific time frame:
/my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"
This will return your TRAN001
and TRAN002
documents.
If possible, change your date format to a sortable form, as Glyn Bird said. I'd suggest to use ISO_8601, this is preferd for JSON (e.g. Javascript Date.toJSON).
If you can't change your data, you can create a view, which transforms your dates into a sortable format.
Example: Put a design doc similar to the following to your database
{
_id: '_design/employees',
views: {
by_action_date: {
map: "function (doc) {n if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docsn var dt = doc.effectiveDateOfAction.split('-'); // parse your date formatn emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as keyn }n }"
}
}
}
The map
function has to be given as string in the document, formatted it is:
function(doc) {
if (doc.effectiveDateOfAction && doc.employeeName) { // filter for employee docs
var dt = doc.effectiveDateOfAction.split('-'); // parse your date format
emit(`${dt[2]}-${dt[1]}-${dt[0]}`); // emit iso date as key
}
}
You can then query it to get your employees sorted:
Use include_docs = true
param to get your real documents included.
/my-database/_design/employees/_view/by_action_date?include_docs=true
You then can also use startkey
and endkey
params, to limit to a specific time frame:
/my-database/_design/employees/_view/by_action_date?include_docs=true&startkey="2018-10-01"&endkey="2018-10-31"
This will return your TRAN001
and TRAN002
documents.
edited Nov 29 at 8:42
answered Nov 28 at 16:03
Enno
1117
1117
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53388100%2fcouch-db-query-to-fetch-the-records-greater-than-a-particular-date%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
"$gt": "10-31-2017"
is a lexicographic string comparison, so10-31-2018
will be greater than01-01-2019
. You may have to create a view, which emits your data sorted by date (see docs).– Enno
Nov 20 at 9:40
@Enno Thank you for the reponse. I am new to couch DB query, If Possible can you please share the working example for this.
– Priyanka
Nov 20 at 11:33