下面的查询在 Studio 3T 和 Robomongo 中运行良好,但我想将其转换为 PHP 格式,
以下是我的查询,
db.news.aggregate([
{
$match: { "_id" : "1" }
},
{
"$project": {
"comments": {
"$filter": {
"input": "$comments",
"as": "comment",
"cond": { "$eq": [ "$$comment.status", "active" ] }
}
}
}
}, {
"$project": {
"comments": {
"$slice": [
{
"$slice": [
"$comments",
{
"$subtract": [ { "$size": [ "$comments" ] }, 1 ]
}
]
}, -1
]
}
}
}])
我在下面尝试过,但它给出了错误“ Error: localhost:27017: FieldPath field names may not be empty strings.
”
PHP转换示例:
<?php
$commentsAggregate=array(
array('$match' => array("_id" => "1")),
array('$project' => array(
"comments" => array(
'$filter' => array(
"input" => "$comments",
"as" => "comment",
"cond" => array('$eq' => array( "$$comment.status", 'active'))
)))),
array('$project' => array(
"comments" => array(
'$slice' => array(array(
'$slice' => array("$comments",array('$subtract' => array( array( '$size' => array("$comments")),1)))
), -1)
)))
);
$cursor=$collectionNews->aggregate($commentsAggregate);
请帮我转换上面的查询。