3

How do you insert dates in couchdb? As strings?

I have couchdb-1.0.3.

1.

I did this:

$ curl -X PUT 127.0.0.1:5984/misc/doc1  -d '{"date":"2011-13-01T17:30:12+01:00"}'

This works, but this date doesn't exists.

2.

I thought I had to do this:

$ curl -X PUT 127.0.0.1:5984/misc/doc1  -d '{"date":new Date("2011-12-01)}'

But this is invalid JSON.

3.

When I use this format,

$ curl -X PUT 127.0.0.1:5984/misc/doc1  -d '{"date":"2011/12/01 00:00:00"}'

I doesn't work well with this format

$ curl -X GET '127.0.0.1:5984/misc/_design/foo/_view/view1?startkey="2012-02-02"'

Because the document shows up in the result.

Thanks,

Eric J.

ericj
  • 2,138
  • 27
  • 44

1 Answers1

2

I suggest that you use your first format, or possibly the JSON2 standard format, which is most convenient for JavaScript. That is what most people do, and it works well with your example request:

$ curl '127.0.0.1:5984/misc/_design/foo/_view/view1?startkey="2012-02-02"'

To validate your data, use a validation function.

Community
  • 1
  • 1
JasonSmith
  • 72,674
  • 22
  • 123
  • 149
  • Thanks, Jason. I tried, but maybe you can tell me why I get an JSON error message when I do: curl -X PUT 127.0.0.1:5984/misc/doc1 -d '{"date":JSON.stringify(new Date)}' – ericj Apr 29 '12 at 10:26
  • That is Javascript, not JSON. – JasonSmith Apr 29 '12 at 10:37
  • Thanks again. I understand. So how do you create documents with a date in it? (Just a simple example would be great) – ericj Apr 30 '12 at 13:15
  • No problem! But would you mind posting a fresh question? To me, this question here is more about "what is the best date syntax" but your next question sounds like "how should I create dates: server-side, client-side, which programming languages, etc. etc." Of course, only you know the specific questions you need to know. – JasonSmith Apr 30 '12 at 23:55