MongoDB-PHP: JOIN-like query - php

Here are the objects:
courses
{ "name" : "Biology", "_id" : ObjectId("4b0552b0f0da7d1eb6f126a1") }
students
{
"name" : "Joe",
"classes" : [
{
"$ref" : "courses",
"$id" : ObjectId("4b0552b0f0da7d1eb6f126a1")
}
],
"_id" : ObjectId("4b0552e4f0da7d1eb6f126a2")
}
Using the PHP Mongo Class, how do I get all the students that has a biology course?
Thanks

You'll need to query twice. I dont have my environment in front of me, but something similar to what's below. I may have the "nested" portion of the second query incorrect.
// First grab the ID for the course.
$course = $collection->findOne(array("name" => "Biology"));
// Next query the students collection.
$collection->find(array("classes" => array("id" => $course['_id'])));

Related

make a nameless array out 2 diferent tables with children in php

I have 2 tables in my DB. 1 is full with numbers (foreign keys) and other one is full of the data I need. This is how the array should (exactly) look
"array" : [
{
"data" : "blabla"
"data2" : "blabla"
"children" : [
{
"data" : "blabla"
"data2" : "blabla"
"children" : [
{
"data" : "blabla"
"data2" : "blabla"
}
]
},
{
"data" : "blabla"
"data2" : "blabla"
"children" : [
{
"data" : "blabla"
"data2" : "blabla"
}
]
}
]
}
]
and so on, so to say.. now the right order you can find in table 1,where it is lited what the mother ID is. and i need to get the data out of table 2 so be in the array. anyone knows how to do this? i am working in codeigniter
updated with pics:
To do that, you would need to process in two steps:
You fetch each document and it's data from the two tables (let's call them "documents" and "documents_data" to build a flat array of all the documents. This way you don't have to wonder where does the data go anymore :
$results = $this->db->query("SELECT d.*, dd.* FROM documents d LEFT JOIN documents_data dd ON dd.doc_id = d.doc_id");
$documents = $results->result_array();
Now that we have everything about each document, we just need to reorder them in a recursive array using a recursive function, that will call itself with the parent id for each children and build the tree :
function getDocumentTree($documents, $idParent = null)
{
$tree = [];
foreach($documents as $document) {
if($document['doc_doc_id'] == $idParent) {
$document['children'] = getDocumentTree($documents, $document['doc_id']);
$tree[] = $document;
}
}
return $tree;
}
$tree = getDocumentTree($documents);
Note: I haven't tested this code, this is just an idea on how it works, try to understand that (especially the concept of recursivity) and implement it by yourself.

Mongo DB aggregate query in php [duplicate]

This question already has answers here:
How do I do a "NOT IN" query in Mongo?
(4 answers)
Aggregation filter after $lookup
(1 answer)
Closed 4 years ago.
I am new in MongoDB so facing issue in making join query in mongo please help
I have two collection "user" and "user_login_details"
Previously I have below query to fetch record from single collection
$data = $this->mongodb->select()->where(array('instituteId' => $instituteId))->where_not_in('type', array($instituteAdmin))->get('user');
Now I want to fetch records from two collection "user" have single row and for that row multiple entries of login detail present in "user_login_details" collection.
I try with aggregate as below
db.user.aggregate([
{$match: { instituteId: '5954ee6dcf2169d50d51b88d'} },
{
$lookup:
{
from: "user_login_details",
localField: "_id",
foreignField: "user_id",
as: "user_login_details"
}
}
]).pretty()
But need one more condition as per my first query in php
->where_not_in('type', array($instituteAdmin))
How I can add this in aggregate query.
Also above query will return data with multiple login details as below.
{
"_id" : ObjectId("5954ee6dcf2169d50d51b88e"),
"email" : "abc#gmail.com",
"instituteId": "5954ee6dcf2169d50d51b88d",
"type": "Institute Admin"
"user_name" : "SA-011",
"religion" : "",
"caste" : "",
"bloodGroup" : "",
"groups" : "",
"reset_password" : NumberLong(1),
"aws_arn" : "",
"user_login_details" : [
{
"_id" : ObjectId("5b069955df05aeda0f8b4568"),
"user_id" : ObjectId("5954ee6dcf2169d50d51b88e"),
"ip" : "::1",
"my_session_id" : "3199d6fd6ecfef1ec63bc1760115d116",
"login_time" : ISODate("2018-05-24T10:52:05Z"),
"logout_time" : ISODate("2018-05-24T11:04:47Z")
},
{
"_id" : ObjectId("5b069c52df05ae6e358b4567"),
"user_id" : ObjectId("5954ee6dcf2169d50d51b88e"),
"ip" : "::1",
"my_session_id" : "b2d33ea8bc7cc3bb8090fc1dd15f7371",
"login_time" : ISODate("2018-05-24T11:04:50Z"),
"logout_time" : ""
}
]
}
I need only latest record from "user_login_details" collection.
Please help by creating query in PHP as I used "CodeIgniter MongoDB Active Record Library".
Thanks in advance.

Intelligent searching with mongo

I am using phalcon with mongodb. I have the following document in collection:
{
"_id" : ObjectId("547c8b6f7d30dd522b522255"),
"title" : "Test vacancy",
"slug" : "test-vacancy",
"location" : "the-netherlands",
"contract" : "fixed",
"function" : "Test vacancy",
"short_description" : "gdfsgfds",
"description" : "fdsafsdgfsdgdfa",
"promo_text" : "gfdsgdfs",
"company_name" : "gfdsgfsd",
"hits" : 36,
"updated_at" : 1.42685e+09,
}
In controller I am fetching all results by searched phase/query. For example I put example word and output will be all posts with example word in description or title or short_desc etc. Everything is correct but I want sort these posts in specific order. I mean if query will be same as title, this post should be first. Now it is somewhere below.
Can you help me? Thank you in advance.

PHP MongoDB query dynamic values

I want to query the content (text) inside my dynamic values keys, but i can't figure out the easiest way to do this.
So my mongo collection is like this:
{
"_id" : ObjectId("566aecb8f0e46491068b456c"),
"metadatas" : [
{
"schema_id" : "f645fabef0e464e51e8b4567",
"values" : {
"name" : "Test",
"age" : NumberLong(29),
"address" : "Test1"
},
"updated_on" : ISODate("2015-12-11T00:00:00Z")
},
{
"schema_id" : "d745fabef0e464e51e8b4567",
"values" : {
"something_else" : "lipsum"
},
"updated_on" : ISODate("2016-12-11T00:00:00Z")
}
],
}
How can i dynamically query whats inside my values since i cannot do $db->collec->find(array('metadatas.values.name' => $regex)) because i might have some other dynamic key instead of name?
thanks in advance
I ended up saving my keys uniquely on another collection and then building the query and concatenating it before applying based on #Sammaye idea:
$regex = new \MongoRegex("/^$query/i");
# First get all the dynamic keys you need to filter
$keys_to_search = $this->db->metadata_keys->find();
$this->log($keys_to_search);
$query_builder = array('$or'=>array());
foreach ($keys_to_search as $value){
array_push(
$query_builder['$or'],
array('metadatas.values.' . $value['key'] => $regex)
);
}
$this->log($query_builder);
$search_metadata_name = $this->db->filesfolders->find(
$query_builder, array('sql_fileid' => true)
);

MapReduce for MongoDB on PHP?

I'm new to MongoDB and this is my first use of MapReduce ever.
I have two collections: Shops and Products with the following schema
Products
{'_id', 'type': 'first', 'enabled': 1, 'shop': $SHOP_ID }
{'_id', 'type': 'second', 'enabled': 0, 'shop': $SHOP_ID }
{'_id', 'type': 'second', 'enabled': 1, 'shop': $SHOP_ID }
And
Shops
{'_id', 'name':'L', ... }
{'_id', 'name':'M', ... }
I'm looking for a GROUPBY similar statement for MongoDB with MapReduce to retrieve the Shops with name 'L' that have Products with 'enabled' => 1
How can I do it? Thank you.
It should be possible to retrieve the desired information without a Map Reduce operation.
You could first query the "Products" collection for documents that match {'enabled': 1}, and then take the list of $SHOP_IDs from that query (which I imagine correspond to the _id values in the "Shops" collection), put them in an array, and perform an $in query on the "Shops" collection, combined with the query on "name".
For example, given the two collections:
> db.products.find()
{ "_id" : 1, "type" : "first", "enabled" : 1, "shop" : 3 }
{ "_id" : 2, "type" : "second", "enabled" : 0, "shop" : 4 }
{ "_id" : 3, "type" : "second", "enabled" : 1, "shop" : 5 }
> db.shops.find()
{ "_id" : 3, "name" : "L" }
{ "_id" : 4, "name" : "L" }
{ "_id" : 5, "name" : "M" }
>
First find all of the documents that match {"enabled" : 1}
> db.products.find({"enabled" : 1})
{ "_id" : 1, "type" : "first", "enabled" : 1, "shop" : 3 }
{ "_id" : 3, "type" : "second", "enabled" : 1, "shop" : 5 }
From the above query, generate a list of _ids:
> var c = db.products.find({"enabled" : 1})
> shop_ids = []
[ ]
> c.forEach(function(doc){shop_ids.push(doc.shop)})
> shop_ids
[ 3, 5 ]
Finally, query the shops collection for documents with _id values in the shop_ids array that also match {name:"L"}.
> db.shops.find({_id:{$in:shop_ids}, name:"L"})
{ "_id" : 3, "name" : "L" }
>
Similar questions regarding doing the equivalent of a join operation with Mongo have been asked before. This question provides some links which may provide you with additional guidance:
How to join MongoDB collections in Python?
If you would like to experiment with Map Reduce, here is a link to a blog post from a user who used an incremental Map Reduce operation to combine values from two collections.
http://tebros.com/2011/07/using-mongodb-mapreduce-to-join-2-collections/
Hopefully the above will allow you to retrieve the desired information from your collections.
Short answer: you can't do that (with a single MapReduce command).
Long answer: MapReduce jobs in MongoDB run only on a single collection and cannot refer other collections in the process. So, JOIN/GROUP BY-like behaviour of SQL is not available here. The new Aggregation Framework also operates on a single collection only.
I propose a two-part solution:
Get all shops with name "L".
Compose and run map-reduce command that will check every product document against this pre-computed list of shops.

Categories