vendredi 31 juillet 2015

MongoDB find by ids array on params order

I have a dataset, such as:

   {
      "_id" : ObjectId("559a9b9c9d9e9f9g1"),
      "title": "First"
    },
    {
      "_id" : ObjectId("559a9b9c9d9e9f9g2"),
      "title": "Second" 
    },
    {
      "_id" : ObjectId("559a9b9c9d9e9f9g3"),
      "title": "Third"
    },
    {
      "_id" : ObjectId("559a9b9c9d9e9f9g4"),
      "title": "Fourth"
    }  

When I try to find with an array of items, like:

db.snippets.find( { _id: { $in: [ObjectId("559a9b9c9d9e9f9g4"), 
                                 ObjectId("559a9b9c9d9e9f9g1"), 
                                 ObjectId("559a9b9c9d9e9f9g3") ] } } )

Instead of returning the query result in the given array of ids order, it returns based on the persisted data order.

Expected:

    { "_id" : "559a9b9c9d9e9f9g4", ...}, (item 4)
    { "_id" : "559a9b9c9d9e9f9g1", ...}, (item 1)
    { "_id" : "559a9b9c9d9e9f9g3", ...}  (item 3)

Result:

    { "_id" : "559a9b9c9d9e9f9g1", ...}, (item 1)
    { "_id" : "559a9b9c9d9e9f9g3", ...}, (item 3)
    { "_id" : "559a9b9c9d9e9f9g4", ...}  (item 4)

Is there a way to force the returned query to follow the same given array order?

Aucun commentaire:

Enregistrer un commentaire