我有一个PHP-MongoDB项目。我正在使用:
我的排序方法有问题。
我有这个集合(注意['order']属性):
[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
[version] => 0
[title] => i1
[body] => New Lorem Ipsum
[order] => 1
)
我从中得到了:
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)
);
}
正如您所看到的,我正在尝试使用['order']属性对结果进行排序,但返回的数组未按此方式排序,值得注意的是前6个索引都是在循环上创建的时间,最后一个(不按顺序)由一个函数创建,该函数搜索新条目的所需顺序并进行必要的['order']操作以使它们适合。
起初我认为vvvlad连接器的'get'方法不起作用,但是当我尝试时也是如此
db.entries.find().sort({$order:1})
Mongo客户端中的
我认为这与'索引值'存在某种关系,但我不明白为什么这种简单的排序方法不起作用。
提前感谢您的时间:)
答案 0 :(得分:0)
我刚刚在Mongo上运行了以下测试,一切看起来都正确:
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())
我不熟悉vvvlad的代码,但你的shell示例肯定存在问题。
db.entries.find().sort({$order:1})
$order
不正确(无$
)。
您确定vvvlad的代码支持排序吗?