yii2 mongodb - how to find element in collection subarray - mongodb

Array from collection looks like this.
$result = $collection->find();
Array
(
[0] => Array
(
[_id] => MongoDB\BSON\ObjectId Object
(
[oid] => 5c52b90454851c44aa2987e2
)
[name] => Array
(
[date] => 2019-01-31 10:59:48
[value] => DESKTOP-TODTF5E
)
[network_addresses] => Array
(
[0] => Array
(
[ip_1] => 12.21.134
[ip_2] => 50
[mac] => xx:xx:xx:xx:xx:xx
)
[1] => Array
(
[ip_1] => 192.168.0
[ip_2] => 2
[mac] => yy:yy:yy:yy:yy:yy
)
)
)
)
I can find if some mac exist in specific row of sub array like this:
$result = $collection->find(["network_addresses.0.mac" =>
"xx:xx:xx:xx:xx:xx"]);
But I need to check if certain mac exists in any row of subarray, so instead of row index 0 I need to put some asterix or something.
How to do that ?

$query = (new Query)->select(["name"])
->from(['db_name','collection_name'])
->where(["network_addresses" => [ '$elemMatch' =>['mac' => "xx:xx:xx:xx:xx:xx"]]]);
$results = $query->all();

Related

PHP MongoDB Search in Sub-array

Data stored in collection as below
[field_1] => Array
(
[fields] => Array
(
[0] => MongoInt64 Object
(
[value] => 1233
)
[1] => MongoInt64 Object
(
[value] => 1234
)
)
)
I need to search 1234 in field.
I used below code in php to search
$param = array('field_1.fields.$' => 1234);
But this is not working
You need to use the $in query criteria, to find elements within an array
$cursor = $collection->find(array("field_1.fields" => array('$in' => array("1234"))));
This will find all items that have 1234 within "fields"
$in doc: https://docs.mongodb.org/v3.0/reference/operator/query/in/

Illuminate returns results in JSON - why?

I'm using Illuminate to access my database
<body>
<?php
$edges = Edge::all();
foreach ($edges as $key => $value) {
print $value;
}
?>
</body>
browser returns:
{"childID":"A","parentID":"C","updated_at":null,"created_at":null}{"childID":"B","parentID":"E","updated_at":null,"created_at":null}{"childID":"C","parentID":"D","updated_at":null,"created_at":null}{"childID":"C","parentID":"F","updated_at":null,"created_at":null}{"childID":"Y","parentID":"Z","updated_at":"2014-08-07 10:26:54","created_at":"2014-08-07 10:26:54"}
What's odd or unexpected for me at least is that my results are returned in JSON. If you could
shine some light on this, i.e. why this might be, or the reason is...I'd be grateful.
If I dump the results in the browser with:
<?php
print '<pre>';print_r($edges);
?>
produces:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => Edge Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[childID] => A
[parentID] => C
[updated_at] =>
[created_at] =>
)
[original:protected] => Array
(
[childID] => A
[parentID] => C
[updated_at] =>
[created_at] =>
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[1] => Edge Object
(...)
...
)
In terms of the front-end results - I'm really only interested in the:
[childID] => A
[parentID] => C
[updated_at] =>
[created_at] =>
part of the object; only NOT in JSON; in plain text.

File uid in FAL file upload field hook

I've extended ExtendedFileUtilityProcessDataHookInterface and created a hook for FAL file upload field.
class tx_bibusdocuments_fileUploadHook implements TYPO3\CMS\Core\Utility\File\ExtendedFileUtilityProcessDataHookInterface {
public function processData_postProcessAction($action, array $cmdArr, array $result, \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $parentObject){}
}
When I print the "$result" array, I got an array like this;
Array
(
[0] => Array
(
[0] => TYPO3\CMS\Core\Resource\File Object
(
[metaDataLoaded:protected] =>
[metaDataProperties:protected] => Array
(
)
[indexingInProgress:protected] =>
[updatedProperties:protected] => Array
(
)
[indexerService:protected] =>
[properties:protected] => Array
(
[size] => 198218
[modification_date] => 1408449118
[creation_date] => 1408449118
[mime_type] => application/pdf
[name] => HomeTest.pdf
[identifier] => /user_upload/test.pdf
[identifier_hash] => 2bc8d0c4ed9f8a87fb9913af5dcd3977e0102027
[storage] => 1
[folder_hash] => e32a309fabc28dd85f053b65c5bd0da99860eb02
[type] => 5
[sha1] => 8a46595222d30c9cb4bcc48a4901d3e0f05e25ad
[extension] => pdf
[missing] => 0
[uid] => 139856
)
)
)
)
How can I get uid and name of the file from this $result array?
We can iterate the result array like this way;
public function processData_postProcessAction($action, array $cmdArr, array $result, \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility $parentObject){
$files = array_pop( $result );
if ( !is_array( $files ) ) {
return;
}
foreach ( $files as $file ) {
$fileUid .= $file->getUid(); // Uid of the file
$fileName .= $file->getName(); // Name of the file
}
}

MongoDB finding nested objects that meet criteria

I have a MongoDB document that is structured similar to the structure below follows. I am searching based on people.search_columns.surname and people.columns.givenname. So for example, when I search for the given name of "Valentine", I want to get the document back, but Nicholas Barsaloux should not be included.
Data structure:
[_id] => MongoId Object (
[$id] => 53b1b1ab72f4f852140dbdc9
)
[name] => People From 1921
[people] => Array (
[0] => Array (
[name] => Barada, Valentine
[search_columns] => Array (
[surname] => Array (
[0] => Mardan,
[1] => Barada
)
[givenname] => Array (
[0] => Valentine
)
)
)
[1] => Array (
[name] => Barsaloux, Nicholas
[search_columns] => Array (
[surname] => Array (
[1] => Barsaloux
)
[givenname] => Array (
[0] => Nicholas
)
[place] => Array (
)
)
)
)
Here is the code I was working on:
$criteria = array("people" => array(
'$elemMatch' => array("givenname" => "Valentine")
));
$projection = array("people" => true);
$documents_with_results = $db->genealogical_data->find($criteria, $projection)->skip(0)->limit(5);
Currently that code returns zero results.
Since the arrays are nested you cannot use basic projection as you can with find. Also in order to "filter" the array contents from a document you need to "unwind" the array content first. For this you use the aggregation framework:
$results = $db->genealogical_data->aggregate(array(
array( '$match' => array(
'people.search_columns.givenname' => 'Valentine'
)),
array( '$unwind' => '$people' ),
array( '$match' => array(
'people.search_columns.givenname' => 'Valentine'
)),
array( '$group' => array(
'_id' => '$id',
'name' => array( '$first' => '$name' ),
'people' => array( '$push' => '$people' )
))
));
The point of the first $match stage is to reduce the documents that possibly match your criteria. The second time is done after the $unwind, where the actual "array" items in the document are "filtered" from the results.
The final $group puts the original array back to normal, minus the items that do not match the criteria.

Search in multiple array in mongodb

I want to Search a value in an array Like
`[_id] => MongoId Object (
[$id] => 511f4ce622efc34f15000001
)
[metadata] => Array (
[filename] => 6410-funny_face.gif
[parrent] => myfolder/newfolder2
[user] => Array ( >>>>>>>> i need to fetch all users
[root] => 7
[admin] => 7
[user] => 0
)
[group] => Array (
[23] => 2
)
)
[filename] => 6410-funny_face.gif
[uploadDate] => MongoDate Object (
[sec] => 1361005798
[usec] => 799000
)
[length] => 3083
[chunkSize] => 262144
[md5] => eb3846f78f461165e5bf59a05707edd1`
I need to find the user's key and value in PHP or MongoShell
im using db.collection.find(array(metadata.filename: 6410-funny_face.gif)); gives the correct answer but while in finding db.collection.find(array(metadata.user: array(7)));
db.collection.find({"metadata.user.root":7})
This should work if I understand your question.