Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to insert ISODate in MongoDB #50

Closed
dvergnes opened this issue Feb 7, 2013 · 3 comments
Closed

How to insert ISODate in MongoDB #50

dvergnes opened this issue Feb 7, 2013 · 3 comments

Comments

@dvergnes
Copy link

dvergnes commented Feb 7, 2013

I have a simple structure with a date as field. Naively, I thought I can do something like this:
{
"todo":[
{
"_class":"experiments.entity.Todo",
"_id":1,
"completed":false,
"content":"test",
"order":1,
"title":"title",
"username":"username",
"lastUpdate":ISODate("2013-02-07T01:15:01.090Z")
}
]
}

But I get an exception when parsing:
Caused by: com.mongodb.util.JSONParseException:
{ "todo":[ { "_class":"experiments.entity.Todo", "_id":1, "completed":false, "content":"test", "order":1, "title":"title", "username":"username", "lastUpdate":ISODate("2013-02-07T01:15:01.090Z") } ]}
at com.mongodb.util.JSONParser.parse(JSON.java:198)
at com.mongodb.util.JSONParser.parseObject(JSON.java:231)
at com.mongodb.util.JSONParser.parse(JSON.java:195)
at com.mongodb.util.JSONParser.parseArray(JSON.java:539)
at com.mongodb.util.JSONParser.parse(JSON.java:191)
at com.mongodb.util.JSONParser.parseObject(JSON.java:231)
at com.mongodb.util.JSONParser.parse(JSON.java:195)
at com.mongodb.util.JSONParser.parse(JSON.java:145)
at com.mongodb.util.JSON.parse(JSON.java:81)
at com.mongodb.util.JSON.parse(JSON.java:66)
at com.lordofthejars.nosqlunit.mongodb.DefaultInsertionStrategy.parseData(DefaultInsertionStrategy.java:34)

I guess it's because my file is not compliant with json format. So, my question is how to insert date in unit test using nosql-unit?

@lordofthejars
Copy link
Owner

Hello, well there is a problem with this kind of data, but is not related to NoSQLUnit but java mongodb driver, if you look next issue JIRA it talks about similar problem but in queries. I think that should work with insertion too. Moreover in stackoverflow it is explained that ISODate is simply a helper class that wraps a Javascript date object.
As discussed in this forum (http://grokbase.com/t/gg/mongodb-user/129ywkf8sq/java-date) you can use Date to retrieve back the date.

I will write a unit test about this to validate all this information I have provided, and depending on results I will close the issue or fix the problem. But as you noted in description it seems more a problem with json file than NoSQLUnit.

Thank you very much for using NoSQLUnit, for any question , problem, ... do not hesitate to contact me.
Alex.

@dvergnes
Copy link
Author

dvergnes commented Feb 7, 2013

Thanks for your post. Actually, using explanation provided in JIRA issue, I succeed to insert date. The final file is:
{
"todo":[
{
"_class":"experiments.entity.Todo",
"_id":1,
"completed":false,
"content":"test",
"order":1,
"title":"title",
"username":"username",
"lastUpdate":{ "$date" : "2013-02-07T09:09:09.212Z"}
}
]
}

It seems to me you can close this issue without trouble but I prefer to let you do so.

@lordofthejars
Copy link
Owner

Good, thank you very much, I will update documentation by providing an example for ISODates.

pranav-patil referenced this issue in pranav-patil/cucumber-framework May 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants