• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java Mongodb update array and sort

 
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
 
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
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic