PHP / MongoDB:如何根据id列表中是否存在userId(String)来排序文档?

时间:2015-11-17 19:41:45

标签: php android regex mongodb sorting

假设我在名为“posts”的集合中有以下文档:

{  
   "_id":ObjectId("56454e4a32d6b633753e455b"),
   "comment":"Give it 6 - 8 years and this duo will",
   "userid":"5643ad3b3185a",
},
{  
   "_id":ObjectId("22224e4a32d6b633753e455b"),
   "comment":"Give it 6 - 8 years and this duo will",
   "userid":"2222ad3b3185a",
},
{  
   "_id":ObjectId("11114e4a32d6b633753e455b"),
   "comment":"Give it 6 - 8 years and this duo will",
   "userid":"1111ad3b3185a",
}

我希望根据id列表中是否存在“userid”字段来对这三个文档进行排序。例如。

$listIds=[8888883b3185a,999999d3b3185a, 2222ad3b3185a]

.sort("_id":-1, **array("userid":array("$in":$listIds))**)

我知道排序的第二部分是错误的,但我怎样才能完成我想要做的那个目标,即根据用户ID是否属于id列表来排序文档?

排序后的所需输出:

{  
   "_id":ObjectId("22224e4a32d6b633753e455b"),
   "comment":"Give it 6 - 8 years and this duo will",
   "userid":"2222ad3b3185a",
},
{  
   "_id":ObjectId("56454e4a32d6b633753e455b"),
   "comment":"Give it 6 - 8 years and this duo will",
   "userid":"5643ad3b3185a",
},
{  
   "_id":ObjectId("11114e4a32d6b633753e455b"),
   "comment":"Give it 6 - 8 years and this duo will",
   "userid":"1111ad3b3185a",
}

带有“userid”的文档:“2222ad3b3185a”将是第一个,因为id存在于ID列表中。

0 个答案:

没有答案