mongodb insert field in single array document - php

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" } })

Related

JSON PHP server modified

The following is my JSON data.
[
{
"id": 1,
"Name": {
"FirstName": "Wilmer",
"LastName": "Crona",
"FullName": "Mr. Cameron Prosacco"
},
"Address": {
"Address1": "84154 Vickie Burg Apt. 994",
"Address2": "Suite 339",
"ZipCode": "89080-0376"
},
"Phone": "316-269-7694 x1364"
},
{
"id": 2,
"Name": {
"FirstName": "Mercedes",
"LastName": "Kshlerin",
"FullName": "Dr. Kellie Bashirian"
},
"Address": {
"Address1": "12638 Cali Spurs",
"Address2": "Suite 353",
"ZipCode": "76622"
},
"Phone": "319-329-3169 x8848"
}
]
Here is where I want my JSON like this: https://github.com/Zlob/php-json-server but in the method. The JSON file must one file and process only one JSON (db.json).
If my JSON file json1.json , json2.json, etc. How should I do?
You can't decode only first element, you can decode all and get first element only, here is online compiler
<?php
$json = '[
{
"id": 1,
"Name": {
"FirstName": "Wilmer",
"LastName": "Crona",
"FullName": "Mr. Cameron Prosacco"
},
"Address": {
"Address1": "84154 Vickie Burg Apt. 994",
"Address2": "Suite 339",
"ZipCode": "89080-0376"
},
"Phone": "316-269-7694 x1364"
},
{
"id": 2,
"Name": {
"FirstName": "Mercedes",
"LastName": "Kshlerin",
"FullName": "Dr. Kellie Bashirian"
},
"Address": {
"Address1": "12638 Cali Spurs",
"Address2": "Suite 353",
"ZipCode": "76622"
},
"Phone": "319-329-3169 x8848"
}
]';
$data = json_decode($json);
$firstObject = $data[0];
var_dump($firstObject);
pass database name in url and set the db name in config. put all your json file inside db folder.
namespace App\Http\Controllers;
use Request;
use Response;
use Config;
use JsonServer\JsonServer;
class JsonServerController extends Controller
{
public function handleRequest($db, $uri)
{
$data = Request::all();
$method = Request::method();
$pathToJson = storage_path($db .'.json'); //if your path in inside storage folder of laravel
Config::set('pathToDb', $pathToJson); //here we set db
$jsonServer = new JsonServer();
$response = $jsonServer->handleRequest($method, $uri, $data);
$response->send();
}
}
Now define in routes
Route::any('api/{db}/{all}', "JsonServerController#handleRequest")->where('all', '.*');
Now the request will be like this for json1 database
GET api/json1/posts
POST api/json1/posts
PUT api/json1/posts/1
PATCH api/json1/posts/1
DELETE api/json1/posts/1
For json2 database
GET api/json2/posts
POST api/json2/posts
PUT api/json2/posts/1
PATCH api/json2/posts/1
DELETE api/json2/posts/1

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"
}
}
}
}

Naming Objects in Mongo Array?

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" })

Foreach empty from json

I have the following url:
https://graph.facebook.com/123456/likes?access_token=__ACCESS_TOKEN__&format=json
which I then do:
$likesList = json_decode(file_get_contents("https://graph.facebook.com/123456/likes?access_token=$access_token&format=json"),true);
which produces e.g.
{
"data": [
{
"name": "yo yo yo",
"category": "Entertainer",
"id": "45640987076",
"created_time": "2012-04-18T16:14:09+0000"
},
{
"name": "Tony Smith",
"category": "Musician/band",
"id": "456456456456",
"created_time": "2012-02-22T06:56:18+0000"
},
{
"name": "Stations",
"category": "Company",
"id": "567657567",
"created_time": "2012-01-30T23:08:39+0000"
}
]
}
and I then want to list e.g. all the names returned so:
foreach ($likesList->data as $element2){
$name = $element2[name];
echo $name;
}
But it's empty?
See this visualization of your data structure.
As you are receiving an array, you need $list["data"] and not $list->data. Also don't forget to quote the array key "name".
foreach ($likesList['data'] as $element2){
$name = $element2['name'];
echo $name;
}
After json_decode with parameter true you will have associative array. You can access to value by string key. Like in example above.

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