MongoDB Find All Comments - php

If I have several documents that use the following structure:
{
"_id" : ObjectId("4f9c6fe033cddec1bea430f2"),
"title" : "Product #1",
"comments" :
[
{
"comment" : "My comment",
"date" : "2012-03-01 00: 00: 00",
"name" : "Test Name",
"username" : "Test Username"
},
{
"comment" : "My comment",
"date" : "2012-03-01 00: 00: 00",
"name" : "Test Name 2",
"username" : "Test Username 2"
}
]
}
Is it possible for me to find all documents that have reviews.name and update it? For example say I want to find all documents with user.name : test name 2 and change it to Cassie. Is that possible? If not would it be better to put the comments in a different collection?

Yes it is possible. You would have to use comments.username to find it and then comments.$.username note that since you are posting to an array you have to tell mongo to go in there using the $ positional operator

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;

how do I get star rating in google search result underneath my site URL

I want to get them just with code. Not with google reviews cause people need to have a google account for that. I've looked on schema.org but i just don't get it.
so how do i get the stars in my snippet
You should use AggregateRating :
<script type="application/ld+json">
{
"#context" : "http://schema.org",
"#type" : "LocalBusiness",
"name" : "My Company",
"url" : "http://www.mycompany.com/",
"telephone" : "(+33) 1 23 45 67 89",
"email" : "info#mycompany.com",
"address" : {
"#type" : "PostalAddress",
"streetAddress" : "1 Rue de la Place",
"addressLocality" : "Paris",
"postalCode" : "75000"
},
"aggregateRating" : {
"#type" : "AggregateRating",
"ratingValue" : "4.2",
"bestRating" : "5",
"ratingCount" : "100"
},
"openingHours": "Mo-Su 00:00-00:00"
}
</script>
The snippet above should be put just before the ending </html> tag, and should display a score of 4.2/5 based on 100 ratings

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.

NumberLong() in MongoDB && PHP

db.projects.find()
{
"_id" : ObjectId("508d4028e60d8c2154a2eb36"),
"date" : NumberLong(1351432927),
"deadline" : "2012-12-31",
"payment" : "100500",
"title" : "Project",
"userid" : ObjectId("5083677d2f5c849509c3aae6")
}
All right, but in PHP print_r($find_result) doesn't show the key "date" at all!
Why? And how I can extract value from NumberLong() in PHP?

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