Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MongoDB中,当某些文档缺少密钥时,如何进行排序?_Mongodb_Mongoid - Fatal编程技术网

在MongoDB中,当某些文档缺少密钥时,如何进行排序?

在MongoDB中,当某些文档缺少密钥时,如何进行排序?,mongodb,mongoid,Mongodb,Mongoid,我有一些文章使用.sort({rating:-1}) 我有很多文章没有任何评级(在DB端,他们甚至没有评级键)。在前端,我以0.5/1的速率显示它们 当我对文章进行排序时,我现在有以下顺序: 1 1 0.89 0.7 0.5 0.2 0.1 0 0 0 0 0 no_key no_key no_key no_key no_key no_key... 但是,我想考虑的是,没有一个_键的评级是0.5级,而不是0级(在前面的排序中甚至更低)。所以,我想要这样的东西: 1 1 0.89 0.7 0.5

我有一些文章使用
.sort({rating:-1})

我有很多文章没有任何评级(在DB端,他们甚至没有评级键)。在前端,我以0.5/1的速率显示它们

当我对文章进行排序时,我现在有以下顺序:

1 1 0.89 0.7 0.5 0.2 0.1 0 0 0 0 0 no_key no_key no_key no_key no_key no_key...
但是,我想考虑的是,没有一个_键的评级是0.5级,而不是0级(在前面的排序中甚至更低)。所以,我想要这样的东西:

1 1 0.89 0.7 0.5 no_key no_key no_key no_key no_key no_key... 0.2 0.1 0 0 0 0 0 
我希望避免将未评级文章缺少的评级键填充0.5


有什么把戏吗?有什么想法吗?

因此,这需要三个单独的查询,但在聚合框架()出现之前,这可能是最好的方法:

> db.test.save({rating:1});
> db.test.save({rating:2});
> db.test.save({rating:3});
> db.test.save({rating:4});
> db.test.save({rating:0.5});
> db.test.save({rating:0});
> db.test.save({rating:0}); 
> db.test.save({rating:0});
> db.test.save({rating:-1});
> db.test.save({});
> db.test.save({});
> db.test.save({});
> db.test.find({rating:{$gte:.5}}).sort({rating:-1});
{ "_id" : ObjectId("4f52c707621918e231bc55da"), "rating" : 4 }
{ "_id" : ObjectId("4f52c705621918e231bc55d9"), "rating" : 3 }
{ "_id" : ObjectId("4f52c703621918e231bc55d8"), "rating" : 2 }
{ "_id" : ObjectId("4f52c700621918e231bc55d7"), "rating" : 1 }
{ "_id" : ObjectId("4f52c70b621918e231bc55db"), "rating" : 0.5 }
> db.test.find({rating:{$exists:false}});
{ "_id" : ObjectId("4f52c718621918e231bc55e0") }
{ "_id" : ObjectId("4f52c719621918e231bc55e1") }
{ "_id" : ObjectId("4f52c71a621918e231bc55e2") }
> db.test.find({rating:{$lt:.5}}).sort({rating:-1});
{ "_id" : ObjectId("4f52c70e621918e231bc55dc"), "rating" : 0 }
{ "_id" : ObjectId("4f52c712621918e231bc55dd"), "rating" : 0 }
{ "_id" : ObjectId("4f52c712621918e231bc55de"), "rating" : 0 }
{ "_id" : ObjectId("4f52c714621918e231bc55df"), "rating" : -1 }

因此,这需要三个单独的查询,但在聚合框架()出现之前,这可能是最好的方法:

> db.test.save({rating:1});
> db.test.save({rating:2});
> db.test.save({rating:3});
> db.test.save({rating:4});
> db.test.save({rating:0.5});
> db.test.save({rating:0});
> db.test.save({rating:0}); 
> db.test.save({rating:0});
> db.test.save({rating:-1});
> db.test.save({});
> db.test.save({});
> db.test.save({});
> db.test.find({rating:{$gte:.5}}).sort({rating:-1});
{ "_id" : ObjectId("4f52c707621918e231bc55da"), "rating" : 4 }
{ "_id" : ObjectId("4f52c705621918e231bc55d9"), "rating" : 3 }
{ "_id" : ObjectId("4f52c703621918e231bc55d8"), "rating" : 2 }
{ "_id" : ObjectId("4f52c700621918e231bc55d7"), "rating" : 1 }
{ "_id" : ObjectId("4f52c70b621918e231bc55db"), "rating" : 0.5 }
> db.test.find({rating:{$exists:false}});
{ "_id" : ObjectId("4f52c718621918e231bc55e0") }
{ "_id" : ObjectId("4f52c719621918e231bc55e1") }
{ "_id" : ObjectId("4f52c71a621918e231bc55e2") }
> db.test.find({rating:{$lt:.5}}).sort({rating:-1});
{ "_id" : ObjectId("4f52c70e621918e231bc55dc"), "rating" : 0 }
{ "_id" : ObjectId("4f52c712621918e231bc55dd"), "rating" : 0 }
{ "_id" : ObjectId("4f52c712621918e231bc55de"), "rating" : 0 }
{ "_id" : ObjectId("4f52c714621918e231bc55df"), "rating" : -1 }

很抱歉,事实上,我已经在这些方面做了一些原型,现在正在使用mongoid。很抱歉,事实上,我已经在这些方面做了一些原型,现在正在使用mongoid。