0

I need to recover one array from an array... I made in mongodb shell with this sentence:

    db.users.aggregate([    
    {$match: {'userQueries.queryId': 'gr591dd6b06149d9.07403049'}},
    {$project: {
        userQueries: {$filter: {
            input: '$userQueries',
            as: 'query',
            cond: {$eq: ['$$query.queryId', 'gr591dd6b06149d9.07403049']}
        }},
        _id: 0
    }}
])

I'm using this driver: https://secure.php.net/manual/en/set.mongodb.php

My document looks like this:

    {
        "_id" : ObjectId("591dd67cc4ef6009ae38e292"),
        "userId" : 0,
        "email" : "dmance@dmance.org",
        "password" : "dmance",
        "fullName" : "daniel",
        "organization" : "dmance",
        "userQueries" : [
                {
                        "queryResultArray" : [
                                {
                                        "docsArray" : [
                                                {
                                                        "hgnc_id" : "HGNC:26970"                                                        
                                                }
                                        ],
                                        "queryResultId" : 0,
                                        "terms" : [
                                                "COX2",
                                                "COX20"
                                        ]
                                },
                                {
                                        "docsArray" : [
                                                {
                                                        "hgnc_id" : "HGNC:130"                                                        
                                                }
                                        ],
                                        "queryResultId" : 1,
                                        "terms" : [
                                                "ACT"
                                        ]
                                },
                                {
                                        "docsArray" : [
                                                {
                                                        "hgnc_id" : "HGNC:7422"
                                                }
                                        ],
                                        "queryResultId" : 2,
                                        "terms" : [
                                                "COX3"
                                        ]
                                }
                        ],
                        "queryId" : "gr591dd6b06149d9.07403049",
                        "queryName" : "Unnamed query"
                },
                {
                        "queryResultArray" : [
                                {
                                        "docsArray" : [
                                                {
                                                        "hgnc_id" : "HGNC:26970"                                                        
                                                }
                                        ],
                                        "queryResultId" : 0,
                                        "terms" : [
                                                "COX2",
                                                "COX20"
                                        ]
                                },
                                {
                                        "docsArray" : [
                                                {
                                                        "hgnc_id" : "HGNC:130"                                                        
                                                }
                                        ],
                                        "queryResultId" : 2,
                                        "terms" : [
                                                "COX3"
                                        ]
                                }
                        ],
                        "queryId" : "gr591de08337e901.19728995",
                        "queryName" : "Unnamed query"
                }
        ]
}

I want to get only the array matching the queryId field.

As I said I made it in the mongo shield but i cant translate it into php.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
dmance
  • 628
  • 1
  • 8
  • 26

1 Answers1

0

It was already answered. I'm sorry. This is how I solved the problem.

    $mongo = new MongoDB\Driver\Manager("mongodb://localhost:27017");
    $pipeline = [
                [
                    '$match' =>
                    [
                        'userQueries.queryId' => 'gr591dd6b06149d9.07403049',
                    ],
                ],
                [
                    '$project' =>
                    [
                        'userQueries' => [                                    [
                                    '$filter' => [
                                        'input' => '$userQueries',
                                        'as' => 'query',
                                        'cond' => [
                                            '$eq' => [
                                                '$$query.queryId', 'gr591dd6b06149d9.07403049',
                                            ]
                                        ],
                                    ],
                                ],                                
                        ],
                        '_id' => 0
                    ],
                ],
    ];

    $command = new \MongoDB\Driver\Command([
        'aggregate' => 'users',
        'pipeline' => $pipeline
    ]);

    $cursor = $mongo->executeCommand('genenames', $command);
dmance
  • 628
  • 1
  • 8
  • 26