MongoDB PHP nested documents - php

ok lets say i have this collection in my tv database
"season" : "1", "episodes" : {"code" : ["1x01", "1x02", "1x03"], "title" : ["Dont Look Back", "Genesis", "Third"]},
"season" : "2", "episodes" : {"code" : ["2x01", "2x02", "2x03"], "title" : ["D2ont Look Back", "G2enesis", "T2hird"]},
"season" : "3", "episodes" : {"code" : ["3x01", "3x02", "3x03"], "title" : ["D3ont Look Back", "G3enesis", "T3hird"]},
"season" : "4", "episodes" : {"code" : ["4x01", "4x02", "4x03"], "title" : ["D4ont Look Back", "G4enesis", "T4hird"]}
how do i make it so that only, lets say the episodes from season 2 are shown?
ive been trying using
echo $obj['episodes']['code'][0];
but it only shows episodes from the last row
im pretty sure my nesting is all wrong but im new to mongo and im having trouble trying to map this out
any advice?

You need to use find() first.
$a = $coll->findOne(array('season' => '2');
That will return an array $a which will have keys 'episodes', 'title'
So once you find the document with findOne, you would then just access the data using $a['title'] or whatever fields you need

Related

Does Laravel's Request object have any default field 'title'?

In our api when we update a group we send the request in following way
{
"title" : "test",
"description" : "test description",
"date_time" : 45525465,
"interest" : "1",
"age_group" : [3],
"capacity" : "10",
"ethnicity" : [],
"privacy_type" : "1",
}
There are several other key-value pairs. By default only those fields are updated which are provided in the request. For example if i want to update only privacy_type I only need to send
{
"privacy_type" : "2"
}
The problem is that when I try to update privacy_type like this, title is also being updated with the value of the route. If I do dd($request->input('title') , I get "api/v1/groups/10024" which is the route for updating a group. Changing title to group_title is an option, but I would like to know if there is any other option. Thanks
You just need a proper ternary:
$group->group_title = $request->has('title') ? $request->get('title') : $group->group_title;

Inserting multiple json elements into document using CodeIgniter MongoDB

I want to insert json array elements like below using MongoDB and CodeIgniter.
"ticket" : [
{
"problem" : "testing",
"time_taken" : "5 hrs",
"number_of_visits" : "7",
"expenses" : {
"amount" : "2000",
"distance_travelled" : "4 km"
}
},
{
"problem" : "testing",
"time_taken" : "5 hrs",
"number_of_visits" : "4",
"expenses" : {
"amount" : "2500",
"distance_travelled" : "5 km"
}
}
]
What is the query I have to use for this?
What you need is the Bulk Write functions (https://docs.mongodb.com/manual/core/bulk-write-operations/)
The PHP depends on which library you are using (old or new, hopefully the new one!) but in plain English you:
create an array of operations (in your case "inserts")
call bulk write.
$this->mongo_db->where('_id', new MongoId($id))->push("ticket",$data)->update('engineer');
This is the query I'm using for inserting multiple array values in mongodb codeigniter.

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.

how to remove longnumber from json value

im using an external mongodb to get json results. I have like 1.000.000 arrays that contain this (this is 1 of those million arrays):
{
"name" : "A Bubble",
"summonerId" : NumberLong(41119375),
"region" : "euw",
"level" : NumberLong(30)
}
how do I remove the Numberlong() from the code? needs to happen for x amount of arrays. Im working with php files.
this is what i want:
{
"name" : "A Bubble",
"summonerId" : 41119375,
"region" : "euw",
"level" : 30
}

Mongo and Yii -> update with $set a field in all the arrays of a subdocument

I'm having problems updating a specific field in all the arrays of a subdocument. I have the following structure in MongoDB:
{
"_id" : ObjectId("539c9e97cac5852a1b880397"),
"DocumentoDesgloseER" : [
{
"elemento" : "COSTO VENTA",
"id_rubroer" : "11",
"id_documento" : "45087",
"abreviatura" : "CV",
"orden" : "1",
"formula" : "Cuenta Contable",
"tipo_fila" : "1",
"color" : "#FFD2E9",
"sucursal" : "D",
"documentoID" : "0",
"TOTAL" : "55426.62",
},
{ ... MORE OF THE SAME ... }
],
"id_division" : "2",
"id_empresa" : "9",
"id_sucursal" : "37",
"ejercicio" : "2008",
"lastMonthNumber" : NumberLong(6),
}
I need to update the field "documentoID" to a specific value; like "20" for example, in all the arrays of the subdocument "DocumentoDesgloseER". How I can do this?
I tried the following (with $ operator) and is not working:
$querySearch = array('id_division'=>'2', 'id_empresa'=>'9', 'id_sucursal'=>'37', 'ejercicio'=>'2008');
$queryUpdate = array('$set'=>array('DocumentoDesgloseER.$.documentoID'=>'20'));
Yii::app()->edmsMongoCollection('DocumentosDesgloseER')->update($querySearch,$queryUpdate);
By the way, I'm using Yii Framework to make the connection with Mongo. Any help or advice is welcome.
Thanks ;D!
Unfortunately, you can't currently use a positional operator to update all items in an array. There is a ticket opened in the MongoDB JIRA about this issue.
There a two "solutions":
Change your schema so that your embedded documents are in the separate collection (it's probably not what you want).
The best you can do, if you don't want to change your schema, is to update each subdocument in PHP and then save the whole document.

Categories