MongoDB sort works only on a section of the collection
I have a PHP-MongoDB project. I'm using:
- CodeIgniter
- PHP MongoDB Driver
- Xampp
- The MongoDB Connector from vvvlad
I have a problem with the sorting method.
I have this collection (notice the ['order'] property):
[4d642296bb3dfde40c000002] => Array
(
[_id] => MongoId Object
(
[$id] => 4d642296bb3dfde40c000002
)
[project_id] => 4d642296bb3dfde40c000001
[parent_id] => 4d642296bb3dfde40c000001
[version] => 0
[title] => Introducción
[body] => Introducción Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[order] => 0
)
[4d642296bb3dfde40c000003] => Array
(
[_id] => MongoId Object
(
[$id] => 4d642296bb3dfde40c000003
)
[project_id] => 4d642296bb3dfde40c000001
[parent_id] => 4d642296bb3dfde40c000001
[version] => 0
[title] => Marco Contextual
[body] => Marco Contextual Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[order] => 2
)
[4d642296bb3dfde40c000004] => Array
(
[_id] => MongoId Object
(
[$id] => 4d642296bb3dfde40c000004
)
[project_id] => 4d642296bb3dfde40c000001
[parent_id] => 4d642296bb3dfde40c000001
[version] => 0
[title] => Marco Conceptual
[body] => Marco Conceptual Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[order] => 3
)
[4d642296bb3dfde40c000005] => Array
(
[_id] => MongoId Object
(
[$id] => 4d642296bb3dfde40c000005
)
[project_id] => 4d642296bb3dfde40c000001
[parent_id] => 4d642296bb3dfde40c000001
[version] => 0
[title] => Capitulo I
[body] => Capitulo I Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[order] => 4
)
[4d642296bb3dfde40c000006] => Array
(
[_id] => MongoId Object
(
[$id] => 4d642296bb3dfde40c000006
)
[project_id] => 4d642296bb3dfde40c000001
[parent_id] => 4d642296bb3dfde40c000001
[version] => 1
[title] => Capitulo II
[body] => Capitulo II Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[order] => 5
)
[4d642296bb3dfde40c000007] => Array
(
[_id] => MongoId Object
(
[$id] => 4d642296bb3dfde40c000007
)
[project_id] => 4d642296bb3dfde40c000001
[parent_id] => 4d642296bb3dfde40c000001
[version] => 0
[title] => Conclusión
[body] => Conclusión Lorem ipsum dolor sit amet, consectetur adipiscing elit.
[order] => 6
)
[4d6422a7bb3dfde40c000008] => Array
(
[_id] => MongoId Object
(
[$id] => 4d6422a7bb3dfde40c000008
)
[project_id] => 4d642296bb3dfde40c000001
[parent_id] => 4d642296bb3dfde40c000001
[v开发者_如何学运维ersion] => 0
[title] => i1
[body] => New Lorem Ipsum
[order] => 1
)
That I get from this:
function get_entries(){
$project_id = key($this->projects_model->get_project());
$data=array(
'project_id' => $project_id
);
return $this->mdb->get(
$this->mdb->sinapsisDB->entries,
$data,
array('order'=>1)
);
}
As you can see I'm trying to sort the results using the ['order'] attribute, but the returned array is not sorted that way, is noteworthy that the first 6 indexes are created on a loop all at the same time, and the last one (that is not in order) is created later by a function that searches the desired order for the new entry and make the necessary ['order'] operations to make them fit in place.
At first I thought that the 'get' method from the vvvlad connector was not working but the same happens when I try
db.entries.find().sort({$order:1})
in the Mongo Client
I think this has some relation with 'indexed values' but I don't undestand why this simple sorting method does not work.
Thanks in advance for you time :)
I just ran the following test on Mongo and everything looks correct:
db.foo.drop()
db.foo.insert( { text : "blah", order : 1 } )
db.foo.insert( { text : "bl", order : 0 } )
db.foo.insert( { text : "blahblah", order : 2 } )
printjson(db.foo.find().sort( { order : 1 } ).toArray())
I am not familiar with vvvlad's code but there is definitely a problem with your shell example.
db.entries.find().sort({$order:1})
The $order
is incorrect (no $
).
Are you sure that vvvlad's code supports sorting?
精彩评论