Intelligent searching with mongo - php

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.

Related

add object id mongo db php document

{
"_id" : ObjectId("5844dd1d3627570f004612a5"),
"updatedBy" : ObjectId("57c4799d23be243006e188f8"),
"updatedAt" : ISODate("2016-12-21T10:08:10.211Z"),
"createdBy" : ObjectId("5801b7195248ef0e00948934"),
"createdAt" : ISODate("2016-12-05T03:21:01.660Z"),
"stock" : 50,
"price" : 15000,
"owner" : ObjectId("5801b7195248ef0e00948934"),
"type" : ObjectId("57dce9ad07f96c701c0b24a8"),
"isValid" : true,
"grade" : "Grade A",
"__v" : 0
}
i want to ask how to insert owner data and also type data as ObjectID to mongoDB with php
when i just insert data, it wont changes to object id
anyone?
i got the answer.
just use like this :
$id_param = 1270982704y1024kjh12kj4h; new MongoId($id_param)
thanks a lot your for help

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;

How to query for an empty string in PHP Mongo?

I'd like to query for all documents in my collection where the field client is an empty string. I've seen plenty of examples of how to check if something's not an empty string but I can't find any examples of how to do the opposite. I've tried db.collection.find({client: ""}) but I get back an empty set.
EDIT: The example entry in the collection looks like
{ "_id" : ObjectId("54eb59699e12a795078b80da"), "reportID" : "1472031", "orgID" : "336", "client" : "", "customerID" : NumberLong(1), "address" : "123 main st", "city" : "Grove City", "state" : "OH", "zip" : "43123", "county" : "Franklin", "gpsLatitude" : null, "gpsLongitude" : null, "dateDue" : ISODate("2012-08-18T00:00:00Z"), "dateDueClient" : ISODate("2012-08-18T00:00:00Z"), "dateComplete" : ISODate("2012-08-18T00:00:00Z"), "dateCompleteEstimate" : NumberLong(0), "contractorSubmissionDate" : ISODate("2012-08-19T00:26:23Z"), "createdOn" : ISODate("2012-08-19T00:21:37Z"), "assignedToContractorOn" : ISODate("2012-08-19T00:21:37Z"), "workTypeID" : "6338", "assignedAdmin" : "7880", "contractorID" : "7880", "categoryID" : "0", "historyLength" : NumberLong(0), "invoiceDate" : ISODate("2012-08-18T00:00:00Z"), "submittedToClient" : ISODate("2012-08-18T00:00:00Z"), "paymentContractor" : NumberLong(0), "paymentClient" : NumberLong(0), "contractorIsPaid" : NumberLong(0), "clientIsPaid" : NumberLong(0), "sentinel" : NumberLong(1), "isFrozen" : NumberLong(0), "numTimesClientReady" : "1", "pcrResponses" : [ ] }
There's a lot of fields but the client one is pretty close to the beginning.
The query you posted should work. Just to verify, I inserted that data as a row in a test collection, and it was returned right back out with db.test.find({client: ''})
I'm thinking you may have a problem somewhere else. I've once or twice been in the wrong database when running queries, or had a typo in the collection name. To verify what my data looks like, I'll often times:
> db.tset.find({client: ''});
[] // What? No results? ...backspacebackspacebackspace
> db.tset.find();
[] // What? No results at all for no query? I must be in the.. OH!
> db.test.find();
[] // What? I thought I had a typo in my collection name. What database am I in?
> db
tseting // OH! I keep making that typo..
> use testing
switched to db testing
> db.test.find({client: ''});
[] // What? Still no results? This is weird...
> db.test.insert({client: ''});
> db.test.find({client: ''});
{ "_id" : ObjectId("54f1b5e5d05052fce4fb6684"), "client" : "" }
// Hmm, ok, so there's nothing wrong with the query. The data came up
// So I'm still just in the wrong place? Really?
// Maybe I woke up on the wrong side of the bed today. I should eat lunch.
Sometimes tunnel vision makes you think the problem lies somewhere that it doesn't. Leave a comment if this is or isn't helpful, maybe we can come up with some more troubleshooting ideas.

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.

MongoDB PHP nested documents

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

Categories