• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Mongodb update array and sort

 
Jacky Daniel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to update a mongodb array and also sort the array..I am aware of $push to add, but I could not find update $set with $sort.

"_id" : "1234",
"myarray" : [{
"id" : "myid123",
"date" : 20150105
}],

I want to update "myarray.$.date" for "_id" : "1234" and "myarray.$id" : "myid123" and sort on "myarray.$.date" .
Hope someone will help to get a solution..thanks
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am not sure if I understood you right. Why do you want to sort something while doing an update. You just update the date in "myarray" and while retrieving back sort by whatever field you need to.


Regards,
Amit
 
Jacky Daniel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit, I am trying to update the array with sorted order on Date in the Array, so when i retrieve i can get in Sorted Order
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am trying to update the array with sorted order on Date in the Array

I am not sure if I understand your requirement right.
As per your initial post, you know the "date" that needs to be updated (_id and myarray.id are known). If that is the case what are you trying to sort.
Or is it that your "date" field as well is an array and you want to sort the dates in that array ?

Regards,
Amit
 
Jacky Daniel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Amit,

Yes I am updating date and while/updating the array element, sort the entire array on {date} element, so when I want to retrieve first n elements or last n elements, I will be sure to get on the order of date.

thanks
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
when I want to retrieve first n elements or last n elements, I will be sure to get on the order of date

This is what I tried mentioning earlier, you would "sort" the elements when you retrieve it not when you store them.

regards,
amit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic