1

I have these four documents:

    {
        "_id" : "qbz2nyPccFzYjYmWo",
        "position" : 0,
        "part_order" : 30
    },
    {
        "_id" : "qbz2nyPccFzYjYmWo",
        "position" : 1,
        "part_order" : 34
    },
    {
        "_id" : "EaCSGEKmqfrgFFXBs",
        "position" : 2
    },
    {
        "_id" : "N3Z6NnWhN35dX4MLq",
        "position" : 3,
        "part_order" : 31
    }

I want to order the collection. I tried with:

db.queue.find().sort({part_order:1,position:1})

But it separates the documents with part_order of without part_order. How Can I fix documents in the positions and just order the documents with part_order in a mongo query?

The output has to be:

    {
        "_id" : "qbz2nyPccFzYjYmWo",
        "position" : 0,
        "part_order" : 30
    },
    {
        "_id" : "N3Z6NnWhN35dX4MLq",
        "position" : 3,
        "part_order" : 31
    },
    {
        "_id" : "EaCSGEKmqfrgFFXBs",
        "position" : 2
    },
    {
        "_id" : "qbz2nyPccFzYjYmWo",
        "position" : 1,
        "part_order" : 34
    }
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
kahonmlg
  • 3,839
  • 3
  • 17
  • 21
  • 1
    You probably want to sort like this db.queue.find().sort({position:1, part_order:1}) – joao Jan 21 '15 at 13:56
  • What do you exactly mean by "fix documents in the positions and just order the documents with part_order"? You want to sort documents according to the position and for those with the same position use the part_order? – joao Jan 21 '15 at 14:00
  • @joao if you do for example db.queue.find().sort({position:1, part_order:-1}) the order is not that we wish because it is just ordered by position. Thank you – kahonmlg Jan 21 '15 at 14:01
  • I want to order all documents with part_order but the documents which has not part_order attribute has to have the position set in position attribute like they are fixed. Like they are no moving. – kahonmlg Jan 21 '15 at 14:05
  • @kahonmlg can you post your expected output – Neo-coder Jan 21 '15 at 14:26
  • @yogesh I have added the output now. – kahonmlg Jan 21 '15 at 15:00
  • @joao I hope you understand the question well with this new example. Thanks – kahonmlg Jan 21 '15 at 15:01
  • @JohnnyHK I know, that is why I have the attribute position. – kahonmlg Jan 21 '15 at 15:06
  • 1
    @kahonmlg Oh, I get it now. Thanks for clarifying. You may be best off just sorting by `part_order` and then performing the `position` sort override in your own code. – JohnnyHK Jan 21 '15 at 15:07
  • @JohnnyHK this is not good for me because I use meteor and the reactive property of collections just works with cursors. If I manipulate the output to an array I lose the reactive property of collection. – kahonmlg Jan 21 '15 at 15:14
  • You will have to code this, much as suggested. There is a detailed example in [this answer](http://stackoverflow.com/a/18884223/2313887) and though the question is about aggregation, the real answer is about using [`.publish()`](http://docs.meteor.com/#/basic/Meteor-publish) – Neil Lunn Jan 21 '15 at 23:19
  • I have publish two collections. One with no part_order, another one with part order. I publish both and I process that in client. It is the best way I can do from now. Thanks. – kahonmlg Jan 24 '15 at 20:05

0 Answers0