Fran b
Fran b

Reputation: 3036

Morphia and $sort operator

I have a collection with documents like this:

book {
  title: "hello world",
  chapters: [
    {
      number: "2",
      title: "foo2"
    },{
      number: "1",
      title: "foo1"
    }
  ]
}

I need one of this options:

  1. A query where the document gotten has the array of chapters sorted by chapter.number

  2. A save operation where sort the array before push the document (but by delegation to mongodb)

I have seen $sort operation in official mongodb documentation but I don't know how use it in Morphia. http://docs.mongodb.org/manual/reference/operator/update/sort/

The output should be:

book { title: "hello world", chapters: [ { number: "1", title: "foo1" }, { number: "2", title: "foo2" } ] }

Upvotes: 0

Views: 590

Answers (2)

Karman Valenzuela
Karman Valenzuela

Reputation: 41

According to Mongo $sort Manual :

Solution 1:

if your collection is "book" you can try somewhere like that:

db.book.update( {title: "hello word"},
                { $push: {
                            chapters:{
                                        $each:[],
                                        $sort: {number:1}                                            
                                    }
                        }
                })

Test:

Create document:

db.book.insert({
    title: "hello world",
    chapters: [
        {
          number: "2",
          title: "foo2"
        },{
          number: "1",
          title: "foo1"
        }
    ]
})

Check:

db.book.find().pretty()

Sort chapters by number:

db.book.update( {title: "hello world"},
                { $push: {
                            chapters:{
                                        $each:[],
                                        $sort: {number:1}                                            
                                    }
                        }
                })

Check again:

db.book.find().pretty()

Solution 2:

Else if you really have any collection with the document "book" embedded, this is solution:

db.yourcollection.update( {"book.title": "hello world"},
            { $push: {
                        "book.chapters":{
                                    $each:[],
                                    $sort: {number:1}                                            
                                }
                    }
            })

Test:

Create document:

db.yourcollection.insert({book:{
    title: "hello world",
    chapters: [
      {
        number: "2",
        title: "foo2"
      },{
        number: "1",
        title: "foo1"
      }
    ]
}})

Check:

db.book.find().pretty()

Sort chapters by number:

db.yourcollection.update( {"book.title": "hello world"},
            { $push: {
                        "book.chapters":{
                                    $each:[],
                                    $sort: {number:1}                                            
                                }
                    }
            })

Check again:

db.book.find().pretty()

Upvotes: 0

evanchooly
evanchooly

Reputation: 6233

you can't sort subdocuments like that. You can sort your results, otherwise, as shown here.

Upvotes: 0

Related Questions