根据数组字段大小对文档进行排序

我正在尝试使用名为“empofthemonth”的数组的大小来对每个返回的字段进行排序 这个月的员工获胜。</ p>

这是插入代码:</ p>

  $ db-&gt; users-&gt; insert(
[“firstname”=&gt;“firstname1”,
“lastname”=&gt;“test”,
“email”=&gt;“test@email.org.uk”,
“keyinfo”=&gt; \ n [“age”=&gt; 22,
“性别”=&gt;“男性”,
“年度就业”=&gt; 1,
“empofthemonth”=&gt; [“Apr”]]
]) ;

$ db-&gt; users-&gt; insert(
[“firstname”=&gt;“firstname2”,
“lastname”=&gt;“test2”,
“email”=&gt;“ test@email.co.uk“,
”keyinfo“=&gt;
[”age“=&gt; 24,
”性别“=&gt;”女性“,
”年度就业“=&gt; 5,

“empofthemonth”=&gt; [“Feb”,“Mar”]]
]);

$ db-&gt; users-&gt; insert(
[“firstname”=&gt;“firstname3” ,
“lastname”=&gt;“test2”,
“email”=&gt;“test@email.c om“,
”keyinfo“=&gt;

[“年龄”=&gt; 31,
“性别”=&gt; “女性”,
“岁就业”=&gt; 2,
“empofthemonth”=&gt; [“Jan”,“May”,“Jun”]]
]);
</ code> </ pre>

我意识到聚合,但我无法确定完整的语法。</ p>

To 总结查询结果应该按以下顺序排列:</ p>


  1. firstname3(月份的3个月)</ li>
  2. firstname2(2)</ li >
  3. firstname1(1)</ li>
    </ ol>
    </ div>

展开原文

原文

I'm trying to use the size of an array called "empofthemonth" to sort each field returned by their amount of employee of the month wins.

Here is the insert code:

$db->users->insert(
    ["firstname" => "firstname1", 
     "lastname" => "test", 
     "email" => "test@email.org.uk", 
     "keyinfo" => 
        ["age" => 22,
         "gender" => "Male",
         "yearsemployed" => 1,
         "empofthemonth" => ["Apr"]]
     ]);

$db->users->insert(
    ["firstname" => "firstname2", 
     "lastname" => "test2", 
     "email" => "test@email.co.uk", 
     "keyinfo" => 
        ["age" => 24,
         "gender" => "Female",
         "yearsemployed" => 5,
         "empofthemonth" => ["Feb", "Mar"]]
     ]);

$db->users->insert(
    ["firstname" => "firstname3", 
     "lastname" => "test2", 
     "email" => "test@email.com", 
     "keyinfo" => 
        ["age" => 31,
         "gender" => "Female",
         "yearsemployed" => 2,
         "empofthemonth" => ["Jan", "May", "Jun"]]
     ]);

I realise that aggregation might be used but i cannot work out the full syntax.

To conclude the query results should be in this order:

  1. firstname3 (3 emp of the months)
  2. firstname2 (2)
  3. firstname1 (1)

1个回答



我们需要 $ project </ code> 我们的文档并返回 $ size </ code> 然后 $ sort </ code> 每个文档按”大小“降序排列。 请注意,要访问数组字段,我们需要使用“点符号”。 </ p>

  db.users.aggregate(
[
{“$ project”:{
“firstname”:1,
“lastname”: 1,
“电子邮件”:1,
“keyinfo”:1,
“sz”:{“$ size”:“$ keyinfo.empofthemonth”}
}},
{“$ sort”: {“sz”:-1}}
]

</ code> </ pre>



PHP中的所有内容: </ p>

  $ db-&gt; users-&gt; aggregate(
[
[“$ project”=&gt; [
“firstname”=&gt; 1,\ n“lastname”=&gt; 1,
“email”=&gt; 1,
“keyinfo”=&gt; 1,
“sz”=&gt; [“$ size”=&gt;“$ keyinfo.empofthemonth “]
]],
[”$ sort“=&gt; [”sz“=&gt; -1]]
]

</ </ code> </ pre>
</ div>

展开原文

原文

We need to $project our documents and return the $size then $sort each document by that "size" in descending order. Note that to access the array field, we need to use the "dot notation".

db.users.aggregate(
    [
        { "$project": { 
            "firstname": 1, 
            "lastname": 1, 
            "email": 1, 
            "keyinfo": 1, 
            "sz": { "$size": "$keyinfo.empofthemonth" } 
        }}, 
        { "$sort": { "sz": -1 } } 
    ]
)


Everything in PHP:

$db->users->aggregate(
    [
        [ "$project" => [ 
            "firstname" => 1, 
            "lastname" => 1, 
            "email" => 1, 
            "keyinfo" => 1, 
            "sz" => [ "$size" => "$keyinfo.empofthemonth" ]
        ]], 
        [ "$sort" => [ "sz" => -1 ] ] 
    ]
)

doufu8588
doufu8588 谢谢您的帮助!
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐