Mongodb query '$项目';在我的mongo聚合查询中不起作用

Mongodb query '$项目';在我的mongo聚合查询中不起作用,mongodb-query,php-mongodb,Mongodb Query,Php Mongodb,这是我的文档格式 { "_id" : ObjectId("5af56909b8be3925cf199924"), "pid" : "9354496457", "vid" : NumberLong("34756636617"), "title" : "Ascolour Barnard Stripe Tank Tee-(5032)", "handle" : "ascolour-barnard-stripe-tank-tee-5032", "image"

这是我的文档格式

{
    "_id" : ObjectId("5af56909b8be3925cf199924"),
    "pid" : "9354496457",
    "vid" : NumberLong("34756636617"),
    "title" : "Ascolour Barnard Stripe Tank Tee-(5032)",
    "handle" : "ascolour-barnard-stripe-tank-tee-5032",
    "image" : "https://cdn.shopify.com/s/files/1/1919/3401/products/5032_barnard_stripe_tank_natural_black_5.jpg?v=1493879380",
    "color" : "XL",
    "size" : "NATURAL/BLACK",
    "width" : null,
    "activity" : "",
    "brand" : "Ascolour",
    "style" : "",
    "feature" : "",
    "type" : "Adult Singlets",
    "price" : 1100,
    "compare_at_price" : 1100,
    "qty" : 1,
    "sku" : "5032",
    "visible" : 1
}
{
    "_id" : ObjectId("5af56909b8be3925cf199925"),
    "pid" : "9354496457",
    "vid" : NumberLong("34756636681"),
    "title" : "Ascolour Barnard Stripe Tank Tee-(5032)",
    "handle" : "ascolour-barnard-stripe-tank-tee-5032",
    "image" : "https://cdn.shopify.com/s/files/1/1919/3401/products/5032_barnard_stripe_tank_natural_black_5.jpg?v=1493879380",
    "color" : "2XL",
    "size" : "NATURAL/BLACK",
    "width" : null,
    "activity" : "",
    "brand" : "Ascolour",
    "style" : "",
    "feature" : "",
    "type" : "Adult Singlets",
    "price" : 1100,
    "compare_at_price" : 1100,
    "qty" : 1,
    "sku" : "5032",
    "visible" : 1
}
在这里,我使用不同的“pid”搜索“type”=>“成人单身人士”,并投影文档中的所有列

所以我写了一个查询:

$cursor = $songs->aggregate([
                ['$project' => ['_id' => 1, 'title' => 1, 'size' => 1]],
                ['$match' =>
                    ['$and' => [
                            ['$or' => [
                                    ['title' => $regex],
                                    ['size' => $regex],
                                    ['color' => $regex],
                                    ['brand' => $regex],
                                    ['activity' => $regex],
                                    ['style' => $regex],
                                    ['type' => $regex],
                                    ['feature' => $regex],
                                    ['width' => $regex],
                            ]
                        ],
                            ['visible' => ['$eq' => 1]],
                            ['qty' => ['$gt' => 0]],
                    ]
                ]
            ],
                ['$group' => ["_id" => '$pid']],
                ['$limit' => 10]
        ]);
但是它没有显示任何东西,如果我删除这条线

['$project'=>[''U id'=>1,'标题'=>1,'大小'=>1]]

然后它向我展示了所有不同的pid。但我需要显示所有列w.r.t distinctpid


提前感谢:)

通过这个查询,我解决了它

$cursor = $songs->aggregate([
                ['$match' =>
                    ['$and' => [
                            ['$or' => [
                                    ['title' => $regex],
                                    ['size' => $regex],
                                    ['color' => $regex],
                                    ['brand' => $regex],
                                    ['activity' => $regex],
                                    ['style' => $regex],
                                    ['type' => $regex],
                                    ['feature' => $regex],
                                    ['width' => $regex],
                            ]
                        ],
                            ['visible' => ['$eq' => 1]],
                            ['qty' => ['$gt' => 0]],
                    ]
                ]
            ],
                ['$group' =>
                    ['_id' => '$pid',
                    'originalId' => ['$first' => '$_id'],
                    'title' => ['$first' => '$title'],
                    'brand' => ['$first' => '$brand'],
                    'pid' => ['$first' => '$pid'],
                    'image' => ['$first' => '$image'],
                    'size' => ['$first' => '$size'],
                    'color' => ['$first' => '$color'],
                    'activity' => ['$first' => '$activity'],
                    'style' => ['$first' => '$style'],
                    'type' => ['$first' => '$type'],
                    'feature' => ['$first' => '$feature'],
                    'width' => ['$first' => '$width'],
                    'handle' => ['$first' => '$handle'],
                    'sku' => ['$first' => '$sku'],
                    'visible' => ['$first' => '$visible'],
                    'qty' => ['$first' => '$qty'],
                    'compare_at_price' => ['$first' => '$compare_at_price'],
                ]
            ],
                ['$project' => [
                    '_id' => '$pid',
                    'title' => 1,
                    'brand' => 1,
                    'pid' => 1,
                    'image' => 1,
                    'size' => 1,
                    'color' => 1,
                    'activity' => 1,
                    'style' => 1,
                    'type' => 1,
                    'feature' => 1,
                    'width' => 1,
                    'handle' => 1,
                    'image' => 1,
                    'pid' => '$_id',
                    'sku' => 1,
                    'visible' => 1,
                    'qty' => 1,
                    'compare_at_price' => 1,
                ]
            ],
                ['$limit' => 10]
        ]); 

您还需要投影
pid
。尝试使用
['$project'=>[''u id'=>1',title'=>1',size'=>1',pid'=>1]],
Hi@Constantin Galbenu我使用了pid,但它对我不起作用。将
$match
放在
$project
之前。