0

I am trying to sort a collection by highest number in a field to lowest and save to another collection. I am using monk to connect to the DB.

  • I am pretty new to databases so if there is a more efficient way of doing this please dont hesitate. The reason I asked the question that way is that I would like to have a complete collection for reading while aggregation and building is being done on a back-end collection. – David Richards Sep 13 '14 at 02:14
  • I am using the DB to display json for my IOS app so having it available as much as possible is important. The back-end job is only run every 30 min so I don't think that limiting writes during the query are important. The collection has about 4000 documents and I would like to sort those by a number field in each document. – David Richards Sep 13 '14 at 02:27
  • 1
    I don't think that another collection is what you want. You should edit your question to explain your use case. At the moment the only answer you are going to get is someone directly answering with a find and sort then write to a new collection, but I don't think that is good practice for your application so your clear use case is what you really need to present. Along with an appropriate title. – Neil Lunn Sep 13 '14 at 02:57

1 Answers1

0

This doesn't make sense. MongoDB doesn't store collections in an order according to any field. It just stores them in the order it writes them to disk (natural order), and this can change with later updates, deletes, and inserts. You want to create an index on the sort field and then use the index in queries to iterate through the documents in the correct order if you need to see them in a specific order.

wdberkeley
  • 11,531
  • 1
  • 28
  • 23