0
I need a force. I have a Mongodb database with a document containing other documents, and I need to create a query in PHP from a key in that subdocument. Follows the structure.
{
"_id": "pastadigital",
"v": {
"docs": {
"RG": {
"tit": "RG",
"obr": "S",
"p": "F",
"x": "1"
},
"CPF": {
"tit": "CPF",
"obr": "S",
"p": "F",
"x": "1"
},
"CNPJ": {
"tit": "CNPJ",
"obr": "S",
"p": "J",
"x": "2"
},
}
}
}
There are several documents, until then I simply searched everything and would filter the results in the application. My query today is simply like this:
<?php
$mon = new mongo();
$filter = ["_id" => ['$in' => ["pastadigital"]]];
$query = new MongoDB\Driver\Query($filter, []);
$pastadig = $mon->Conn->executeQuery('sistema.confs', $query)->toArray();
But now the need has arisen to search accordingly for that key "x". So I need to do a search, something like ['v.docs.*.x' => '1']
. With * as any index containing x = 1
I tried with regex or look for some Wildcard for indexes but found nothing. Some light?
It didn’t work is that?
– novic
No, actually I could not mount this query and search only the results from the index 'x'
– Rafael
basically this works:
{$or: [{'v.docs.RG.x': '1'}, {'v.docs.CPF.x': '1'}, {'v.docs.CNPJ.x': '1'}]}
– novic
Yes in this scenario works, but I will not be able to manually inform these documents (RG, CPF) because they are filled dynamically. That’s why I needed a joker in this field and apply the filter directly to x.
– Rafael
I’m looking for already test with
*
and it didn’t work.– novic
How so filled in manually: these documents are different? does not have a standard in the document?
– novic
The * was as an example, I believe it does not serve as a joker. I say that this is a list of documents that is always changed. You won’t always get the number, another day you might get an extra document. So I can’t tell you to search in RG, CPF, etc. I need to replace this document name with a joker. Or some kind of function, that at each document, check if the 'x' is = 1 for example. But with Mongo do not know how to do this.
– Rafael
Next doesn’t have a pattern is also very damaging, but, I’m trying to find a way to do this
– novic
the consultation would be
{ 'v.docs':{'$in':{'x':{'$gte':1}}}}
if v.Docs was an array, like no, it needs to be something much longer as @novic mentioned, I suggest you turn it into an array, after the properties name equals the property 'tit' within it...– anon
explaining: "$in" = iterate array to search of any element that satisfies the query:, query= x for GTE (Greater than or Equal; greater or equal) to 1
– anon