MongoDB how to query a collection with array inside php - php

How can I query a MongoDB with array inside.
[{
"issuer":"34a8c528-11f9-490c-82ef-db94808ba4d8",
"dateAdded":1520547942137,
"duration":2147483647,
"reason":".",
"active":false,
"rank":"5569543b-efc4-4acf-b6b2-cafd3663b806",
"rankName":"Owner"
},{
"issuer":"34a8c528-11f9-490c-82ef-db94808ba4d8",
"dateAdded":1520556569443,
"duration":2147483647,
"reason":".",
"active":true,
"rank":"5569543b-efc4-4acf-b6b2-cafd3663b806",
"rankName":"Owner"
}]
I tried doing:
$result = $collection->find(array("groups.rankName" => "Owner"));
But it returns nothing. Any ideas?

How about $result = $collection->find(array("rankName" => "Owner"));?
By adding groups you're actively searching for an object with that, although I understand that you meant the whole objects containing your rankName property.

Related

how to remove duplicate entry from array objet

Morning I'm working on a project and I have a issue, it's about to know how to remove duplicate entry from an array object .
Here is structure of my array :
"IDGROUP": [
{
"id": 72
},
{
"id": 72
}
]
Here is code snippet:
if($compteGroupes && $compteGroupes->getId()!=0){
$Tableaux_pack[$current_id_fictif]["ingroup"]]=
$Mes_comptes_reels_dependants [$taille_reel];
$Tableaux_pack[$current_id_fictif]["IDGROUP"]=
array(
'id'=>$compteGroupes->getId()
);
}
thank's for your help
Your object looks like a javascript object - what do you actually mean?
are the entries classes? are they arrays
do you want to filter nested/multidimensional arrays based on their values?
do you want to filter different class objects?
see here: https://stackoverflow.com/a/2426579/8548024

create a multidimensional array for pdo select

while ($v = $stmt - > fetch(PDO::FETCH_ASSOC)) {
$basicinfo[] = array('sysid' => $v['sysid'], 'thesis' => $v['thesis']);
}
$input = array_map("unserialize", array_unique(array_map("serialize", $basicinfo)));
echo json_encode(array_values($input), JSON_UNESCAPED_UNICODE);
}
this is how i make an array from database i want to make a multidimensional array that i can access from ajax request like data[i].group[j].leader i want to create another array inside the first array which is named thesis. I want to name that array group to represent the number of group that are taking up the subject thesis. Inside this array i want to put the value of the members of that group. I can also add another array inside the array thesis, it will be an array with same level as the array group. i have tried doing it like this
$basicinfo[$group] = array('sysid' => $v['sysid'], 'thesis' => $v['thesis'],"$group=>array('leader' => $v('leader_name'))");
I want the array to look like this
[{
"sysid": "015-08-0004-001-063-2001",
"subject": "thesis",
"group": {
"groupid": "1",
"groupleader": "John",
},
"adviser": {
"advisername": "Prof Smith",
}
}]
i want it like this so i can access each array in the ajax(jquery) like
data[i].group[j].leader
data[i].adviser[j].advisername
Update
i still need to add more table and i think more columns for now this is what i am working with

Using $all to match values inside an array inside of another array

I have the following mongo document structure
"search": [
[
"keyword",
"match"
],
[
"testing",
"something",
"serious"
]
]
I want to find documents where the array of keywords inside of the array match an $all query.
E.g if search had only 1 level I would do
{'search': {'$all': ['keyword','match']}}
I've tried using:
{'search': {'$elemMatch': {'$all': ['keyword','match']}}}
But I get no results.
If you know the array of keywords in advance and you want to match a document that contains that array inside the search array, you can just use a simple query as follows.
db.collection.find({"search": ["keyword", "match"]});
That should return your sample document. On the other hand, if the array is not completely contained by an element inside search, it will not return anything. For example the following query will not return your sample document.
db.collection.find({"search": ["keyword", "match", "testing"]});

mongo->php, querying inside embeded objects

I have this structure:
"_id": NumberInt(1),
"link_id": {
"1000748": {
"pi": NumberInt(34),
"li": NumberInt(8)
},
"1002836": {
"pi": NumberInt(21),
"li": NumberInt(1002836)
}
}
I want to make a query to select only the link_ids with a 'pi' => 34. I have tried in php $res = $collection->findOne(array("_id" => intval($_catids['categoryid'])), array("linkid.$.pi" => intval(34)));
No success. Any ideas? Thx a lot!
First off I recommend to use the MongoId object for the _id field, has a lot of options and a lot of usefull functions within it
But that did not answer the question, the query is as followed
{
"linked_id.pi": 34
}
Then you translated to PHP it is
array(
'linked_id.pi' => 34,
)
Then depening on the result you want you need to use find or findOne

Why am I getting a dictionary as a result when the result should be an array?

I am using Doctrine in my PHP app to return a result set using the following code
$dm = $this->get('doctrine.odm.mongodb.document_manager');
$query = $dm->createQueryBuilder('SomeBundle:Listing')
->select('title')
->field('userId')->equals(1);
$listings = $query->getQuery()->execute();
$listings_array = $listings->toArray(); <--- WHY NOT RETURNING AN ARRAY?????
$data = array('success'=>true,'listings' => $listings_array, 'displaymessage' => $classifieds->count(). " Listings Found");
What gets out out is the following:
{"success":true,"listings":{"50831582253b4acf09000000":{"id":"50831582253b4acf09000000","title":"fddfds","assets":[],"discussions":[]}},"displaymessage":"1 Listings Found"}
I am wanting an array and not a dictionary.
Any help?
I havent messed with the ODM much but i suspect Doctrine always uses the key for the record as the key in the array when calling toArray on a collection, it makes it easier for most of the cases when you would want to do this, especially since there is no distinction in php between a dict/hash and an array.
Call array_values on it if you want a numerically indexed array.
$data = array(
'success'=>true,
'listings' => array_values($listings_array),
'displaymessage' => $classifieds->count(). " Listings Found"
);

Categories