Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mongo query to get distinct data from db

Tags:

php

mongodb

I have a mongo query to get data from a mongo data base.

db.testDB.distinct("header.app", {"tags": {$exists: true, $in:["Release"]}});

Iam using this to fetch some data from mongo db.

But i need to use this query in php.

As an new to php am not aware of php mongo queries.

How can i convert this to php query

like image 990
Psl Avatar asked May 24 '26 06:05

Psl


1 Answers

Sample conversion to PHP:

$query = ['tags' => ['$exists' => 1, '$in' => ['Release']]]; // your typical MongoDB query

$cmd = new MongoDB\Driver\Command([
  // build the 'distinct' command
 'distinct' => 'testDB', // specify the collection name
 'key' => 'tags', // specify the field for which we want to get the distinct values
 'query' => $query // criteria to filter documents
]);

$cursor = $manager->executeCommand('databasename', $cmd); // retrieve the results

Used with PHP Version 5.6.3, mongodb driver 1.2.9, MongoDB 3.4

like image 154
Bill Zelenko Avatar answered May 26 '26 19:05

Bill Zelenko