Naming Objects in Mongo Array? - php

Using PHP and Mongo I would like to update the users availability but cannot figure it out. How can I structure my collection to be able to reference availability groups.steve.availability?
Below is the structure of my "groups" collection:
{
"_id": {
"$oid": "524327d536b82c7c5c842f6d"
},
"group_id": "testing",
"password": "test",
"users": [
{
"username": "steve",
"availability": "null"
},
{
"username": "joeb",
"availability": "null"
}
]
}

If you want to reference it the way you've suggested: groups.steve.availability, you'd need to structure your documents more like below. (I'm not sure where groups is coming from).
This example would give you users.steve.availability by moving the user's name to a sub-field of the users field (users.steve).
{
"_id": {
"$oid": "524327d536b82c7c5c842f6d"
},
"group_id": "testing",
"password": "test",
"users": {
"steve": {
"availability": "null"
},
"joeb" : {
"availability": "null"
}
}
}
Or, you could just create fields directly on the document:
{
"_id": {
"$oid": "524327d536b82c7c5c842f6d"
},
"group_id": "testing",
"password": "test",
"steve": {
"availability": "null"
},
"joeb" : {
"availability": "null"
}
}
That would allow you to just use steve.availability.
If you're trying to do a query though, you'd be better off leaving it more like you had it originally:
"users": [
{
"username": "steve",
"availability": "null"
}]
So, you could write queries that were like:
db.groups.find({"users.username" : "steve" })

Related

Laravel Query to select list entry in JSON field

I'm storing dynamic data in a MySQL JSON field using Laravel v6.11.0, nova v2.9.3 and nova-flexible-content v0.1.13. The data stored looks similar to this:
[
{
"layout": "source",
"key": "5ce8e0a877487fe5",
"attributes": {
"value": "342",
"unit": "USD",
"language": "en",
"url": "http:\\/\\/google.com",
"authority": "google.com",
"entry_date": "2020-01-21",
"date": "2019-12-21"
}
},
{
"layout": "source",
"key": "a82393ce016e8c14",
"attributes": {
"value": "444",
"unit": "USD",
"language": "en",
"entry_date": "2020-01-21",
"url": "https:\\/\\/google.com",
"authority": "TEST",
"date": "2020-01-20"
}
}
]
I was wondering if it's possible to build a Laravel Query to select the second entry based on the authority entry? Criteria are:
url should contain google.com AND
authority shouldn't be google.com
I've found https://laravel.com/docs/5.8/queries#json-where-clauses, but am struggling to put the right query together. Maybe someone could give me some pointers on how to do it? Thank you
You may refer to this discussion on Laracast
$data = App\YourModel::whereRaw('JSON_CONTAINS(body->"$[*].attributes.url", "\"https://google.com\"")')
->orWhereRaw('JSON_CONTAINS(body->"$[*].id", "\"http://google.com\"")')
->whereRaw('not JSON_CONTAINS(body->"$[*].attributes.authority", "\"google.com\"")')
->get();
foreach ($data as $key => $row) {
$bodyArr = json_decode($row->body);
foreach ($bodyArr as $item) {
if ((preg_match("/(http:\/\/www\.|https?:\/\/google.com)/i",$item->attributes->url)) && (strpos($item->attributes->authority, 'google.com') === false)) {
dump($item);
// YOUR CODE GOES HERE
}
}
}

ElasticSearch php sdk simple query not working

this is my first question here, I am having a problem with a simple elasticsearch query made throught the php sdk, json example:
{
"_id": "event:5569fbbdddc85",
"_type": "event",
"videos": {},
"status": "published",
"owner": {
"firstname": "Patricio",
"lastname": "",
"profilepicture": "http://pbs.twimg.com/profile_images/581193413426544640/Q5aqMmPk_normal.jpg",
"_id": "twitter:2383339241",
"_type": "user",
"updated": 1433008088365,
"created": 1428439794713
},
"max_age": "18",
"min_age": "18",
"max_invites": "5",
"min_invites": "2",
"updated": 1433009134942,
"created": 1433009134942
}
What I need to do is a filter by owner._id and I am doing this:
$params['index'] = 'default';
$params['type'] = 'event';
$params['size'] = $limit;
$params['from'] = $from;
$params['body']['query']['match']['owner._id'] = $userId;
// elasticsearch search query
$res = \Es::search($params);
the result is no filter. All the events in database are comming back.
I am following exactly the docs, but with no results, obviously I am missing something
Thanks!
You need your _id field to be not_analyzed or analyzed with the keyword analyzer so that, when indexed by ES, to stay unchanged.
Also, for a query like yours, for _id it is best to use a filter of type term. I am no php developer, but from ES point of view it should look like this:
"_id": {
"type": "string",
"index": "not_analyzed"
}
And the query should be of this form, for _id:
"query": {
"filtered": {
"filter": {
"term": {
"owner._id": "twitter:2383339242"
}
}
}
}

Can't get username of last follower from array

Trying to get the name of the latest Twitch-follower with help from a snipper I found online. However I get an error message which I can't understand
$json_array2 = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/'.strtolower($channelName).'/follows?limit=1'), true);
$latestFollower = $json_array2['follows']['user']['name'];
Typing in the URL in my broser I get this so I'm thinking follows->user->name should be right but I guess not :(
{
"follows": [
{
"created_at": "2014-07-09T23:30:59Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/username17376/follows/channels/sodapoppin"
},
"user": {
"_id": 65845277,
"name": "username17376",
"created_at": "2014-07-08T03:59:16Z",
"updated_at": "2014-07-08T04:14:14Z",
"_links": {
"self": "https://api.twitch.tv/kraken/users/username17376"
},
"display_name": "Username17376",
"logo": null,
"bio": null,
"type": "user"
}
}
],
"_total": 339627,
"_links": {
"self": "https://api.twitch.tv/kraken/channels/sodapoppin/follows?direction=DESC&limit=1&offset=0",
"next": "https://api.twitch.tv/kraken/channels/sodapoppin/follows?direction=DESC&limit=1&offset=1"
}
}
Should be
$json_array2['follows'][0]['user']['name'];
Based on your JSON
{
"follows": [
{
Which shows that follows is an array. The first object in the the array (index 0) is an object that has the property "user"

mongodb insert field in single array document

I have a document in mongodb that looks like this
{
"_id": ObjectId("5378da275ad972a811c119fb"),
"name": "test test",
"fname": "test",
"lname": "test",
"phone": "13254355554",
"user": "525518965ad972636d7aa0ae",
}
And i want to insert a new field for "employee_id" so the result should be exactly like this.
{
"_id": ObjectId("5378da275ad972a811c119fb"),
"name": "test test",
"fname": "test",
"lname": "test",
"employee_id": "09872",
"phone": "13254355554",
"user": "525518965ad972636d7aa0ae",
}
I have used $push and $addToSet but the results became an array like
"employee_id": {
"0": "09872",
}
Just use $set;
db.test.update(
{ _id:ObjectId("5378da275ad972a811c119fb") }, // Update this id only
{ $set:{"employee_id": "09872"} } // by setting employee_id
)
Use the $set operator, $push and $addToSet are for arrays:
db.collection.update({}, {'$set': { "employee_id": "09872" } })

imploding array_keys from Facebook JSON data

Need some help with the sample code provided the facebook. I can't get it to return a series of IDs that I need to run a sql query against.
$friends = '{
"data": [
{
"name": "Paul",
"id": "12000"
},
{
"name": "Bonnie",
"id": "120310"
},
{
"name": "Melissa",
"id": "120944"
},
{
"name": "Simon",
"id": "125930"
},
{
"name": "Anthony",
"id": "120605"
},
{
"name": "David",
"id": "120733"
}
]
}';
$obj = json_decode($friends);
print $obj->{'data'}[0]->{'name'};
I can return the "Paul"
what I want is to return all the id's using implode(array_keys($obj),",")
I am only getting data to return.
What I'd like to do is retrieve all the IDs separated by a comma.
Thanks!
Try implode-ing on the data key with array_map:
function get_id($o) {
return $o->id;
}
implode(array_map('get_id', $obj->data),",")

Categories