2

I am trouble because I want to increment a date object in 1 hour, with the java driver, this is:

{"tDate":{$add: ["$tDate", 3600*1000]}

making, wont work because mongoDB expects a number and receives a string

String [] date_add_array =  {"$t_tDate", String.valueOf(3600*1000) };
BasicDBObject query_component = new BasicDBObject("tDate", new BasicDBObject("$add", date_add_array))

>exception: $add only supports numeric or date types, not String

Using a BasicDBList object list won't work, because I don't want an object inside the array. This would be (and is not what I want or need):

 {"tDate":{$add: [{"$tDate", 3600*1000}]}

What is the work around? How can I feed MongoDB with an array of mixed data types?

SQL.injection
  • 2,607
  • 5
  • 20
  • 37
  • is this helpful? http://stackoverflow.com/questions/17335037/how-to-define-add-count-count1-aggregation-with-mongo-java-impleme – 3rf Sep 18 '13 at 15:27
  • 1
    I think what the link is saying to do is to just pass in the DBList object without further wrapping that in an array? – 3rf Sep 18 '13 at 15:31
  • @3rf yes, i want to pass an array/list unwrapped. Using DBList will place an array of wrapped objects. **I will get this with BasicDBList**: {"tDate":{$add: [{"$tDate", 3600*1000}]}, **but I want/need this**: {"tDate":{$add: ["$tDate", 3600*1000]}. – SQL.injection Sep 18 '13 at 18:50
  • 1
    The above link seems to suggest something like `BasicDBList dbList = new BasicDBList();` `dbList.add("$tDate);` `dbList.add(3600*1000);` might work. Again I'm just trying to be helpful, I have very limited experience with the java driver. – 3rf Sep 18 '13 at 18:55
  • @3rf I will have a try at that tomorrow when I go back to the office. Cheers mate – SQL.injection Sep 18 '13 at 19:01
  • 1
    it works, yesterday when I tried to use the BasicDBList, i could not add and int (the interface only accepts objects), today I created an **Integer object** and the BasicDBList accepted it :) – SQL.injection Sep 19 '13 at 08:05

0 Answers0