PHP MongoDB Record Searching Issue - php

I have MongoDB Collection with following data
MongoDB Enterprise > db.UCODEPatter.find()
{ "_id" : { "UCODE" : NumberLong("233220524641368") }, "value" : 117 }
{ "_id" : { "UCODE" : NumberLong("233222325602424") }, "value" : 71 }
{ "_id" : { "UCODE" : NumberLong("233222325601932") }, "value" : 69 }
{ "_id" : { "UCODE" : NumberLong("233222325601947") }, "value" : 59 }
{ "_id" : { "UCODE" : NumberLong("233222325602414") }, "value" : 58 }
{ "_id" : { "UCODE" : NumberLong("233222325602419") }, "value" : 49 }
{ "_id" : { "UCODE" : NumberLong("233222325602418") }, "value" : 48 }
{ "_id" : { "UCODE" : NumberLong("233220535710165") }, "value" : 47 }
{ "_id" : { "UCODE" : NumberLong("233222325602405") }, "value" : 46 }
{ "_id" : { "UCODE" : NumberLong("233222325602420") }, "value" : 45 }
{ "_id" : { "UCODE" : NumberLong("233220537026120") }, "value" : 45 }
{ "_id" : { "UCODE" : NumberLong("233222686003297") }, "value" : 44 }
{ "_id" : { "UCODE" : NumberLong("233222325602417") }, "value" : 44 }
i use below php code for data serching
<?php
// Configuration
$dbhost = 'localhost';
$dbname = 'EIRLOGData';
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;
$c_users = $db->UCODEPatter;
$user = array(
"_id" => "\"UCODE\" : NumberLong(\"233220524641368\")"
);
$user = $c_users->findOne($user);
var_dump($user);
?>
when i run it , it return no data , please help me to sort this

I am not too sure why you are storing { "UCODE" : NumberLong("233220524641368") }, "value" : 117 } in your _id, but you could try the following:
$user = array(
"_id.UCODE" => 233220524641368
);

Related

FOSElastica nested query

My collections are like this:
{
"_index" : "test_index",
"_type" : "test_type",
"_id" : "10000",
"_score" : 1.0,
"_source" : {
"user_id" : 12,
"index_date" : {
"date" : "2018-02-06 14:25:49.816952",
"timezone_type" : 3,
"timezone" : "UTC"
},
"rating" : null,
"orders" : [
{
"hour" : "08",
"count" : 1
},
{
"hour" : "10",
"count" : 1
}
],
"products" : [
{
"p_id" : 970111,
"count" : 4
},
{
"p_id" : 1280811,
"count" : 1
},
]
}
},
and tried to access to {"hour":"10"}
My query is:
$query = new Query\Nested();
$query->setPath('orders');
$term = new Term();
$term->setTerm('orders.hour', $order->getCreatedAt()->format('H'));
$query->setQuery($term);
dump($finder->find($query));die;
but i got the following error:
[Elastica\Exception\ResponseException]
failed to create query: {
"nested" : {
"query" : {
"term" : {
"orders.hour" : {
"value" : "12",
"boost" : 1.0
}
}
},
"path" : "orders",
"ignore_unmapped" : false,
"score_mode" : "avg",
"boost" : 1.0
}
} [index: test_index] [reason: all shards failed]
Your documents not look like nested queries.
I assume that finder is your repository manager that is defined as orders repository, your code should look something like this
$finder = $this->get('fos_elastica.repository_manager')->getRepository('YourBundle:order');
$boolquery = new Query\BoolQuery();
$term = new Query\Term();
$term->setTerm('hour', $order->getCreatedAt()->format('H'));
$boolquery->addMust($term);
$finder->find($boolquery);

mongodb update (insert array inside array) PHP

{
"_id" : "9127194b5bcebc099877d6192647412572892576",
"id_store" : "a907b33f4a3141ad2086815d841554b3f7dbe15a",
"created_on" : ISODate("2016-03-01T06:18:39.000Z"),
"product" : [
{
"_id" : "1967e1f0158a8589d49d9ffb8fe5236cf4d4482f",
"kategori" : "pakaian",
"created_on" : ISODate("2016-03-01T06:21:08.000Z"),
"jenis_barang_umum" : [
{
"_id" : "2dc12dec915f1ad013d8bc8610ed8d6cafea6995",
"jenis_barang_umum" : "baju",
"created_on" : ISODate("2016-03-01T07:52:26.000Z")
},
{
"_id" : "8d0fd34d9c96c82fd736772fd02eb01cf13d4561",
"jenis_barang_umum" : "celana",
"created_on" : ISODate("2016-03-01T07:52:32.000Z"),
"jenis_barang_spesifik" : [
{
"_id" : "1b14e9be168dc762713e35c453a91ceb653638ebb",
"jenis_barang_spesifik" : "jeans",
"created_on" : ISODate("2016-03-01T07:52:40.000Z")
}
]
},
{
"_id" : "1b14e9be168dc762713e35c453a91ceb65e638e8",
"jenis_barang_umum" : "jaket",
"created_on" : ISODate("2016-03-01T07:52:40.000Z"),
"jenis_barang_spesifik" : [
{
"_id" : "1b14e9be168dc762713e35c453a91ceb65e638ebb",
"jenis_barang_spesifik" : "jaket kulit",
"created_on" : ISODate("2016-03-01T07:52:40.000Z")
}
]
}
]
},
{
"_id" : "d839e4dd9ba895f7733a6713a653ae9fcf7b79b9",
"kategori" : "aksesoris",
"created_on" : ISODate("2016-03-01T06:21:13.000Z"),
"jenis_barang_umum" : [
{
"_id" : "4855847d579f31c17925218a5723764a8f0fc10a",
"jenis_barang_umum" : "topi",
"created_on" : ISODate("2016-03-01T07:42:36.000Z")
},
{
"_id" : "6a7a917f6e139552e3883594b73a67fb6fa4ad27",
"jenis_barang_umum" : "gelang",
"created_on" : ISODate("2016-03-01T07:47:22.000Z")
},
{
"_id" : "8bd3428f2ac106c5323af1defe125675f08afcf3",
"jenis_barang_umum" : "kalung",
"created_on" : ISODate("2016-03-01T07:47:28.000Z")
}
]
}
]
}
I have document like that, how syntax for inserting new array inside jenis_barang_spesifik (my data inside jenis_barang_spesifik added by robomongo so I dont know how the syntax is).
Here is my syntax for inserting new array into jenis_barang_spesifik but it didn't work. Hope you guys can help me :D
Thanks
$ar_jenis_barang_spesifik = array(
'_id' => hash('sha1', time() . $jenis_barang_spesifik),
'jenis_barang_spesifik' => $jenis_barang_spesifik,
'created_on' => new MongoDate()
);
$result = $jenis_barang_spesifik_collection->update(array('product.jenis_barang_umum._id.' => $jenis_barang_umum), array('$push' => array('product.$.jenis_barang_umum.$.jenis_barang_spesifik' =>$ar_jenis_barang_spesifik)));

MongoCursorTimeoutException with sort on _id

I have an Mongo collection containing ~7 millions events. To get the events that happend for an aggregate I have the following PHP code
$client = new MongoClient();
$db = $client->selectDB('db_name');
$collection = $db->selectCollection('events');
foreach($collection->find([
'headers.for' => '89d115f8-0b2f-470e-9495-2a07d9dfb942',
])->sort([
'headers.occurredOn' => 1,
'_id' => 1,
]) as $event) {
var_dump($event);
}
When I run the above PHP code I get an MongoCursorTimeoutException after 30 seconds.
But when I run the same code without a sort on _id, so:
$client = new MongoClient();
$db = $client->selectDB('db_name');
$collection = $db->selectCollection('events');
foreach($collection->find([
'headers.for' => '89d115f8-0b2f-470e-9495-2a07d9dfb942',
])->sort([
'headers.occurredOn' => 1,
]) as $event) {
var_dump($event);
}
The error does not occur and get instant results (which is one record).
So why does a MongoCursorTimeoutException occur when a sort on _id is added?
The indexes for the collection looks as follow
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "db.events"
},
{
"v" : 1,
"key" : {
"headers.occurredOn" : NumberLong(1),
"_id" : NumberLong(1)
},
"name" : "headers_occurredOn_1__id_1",
"ns" : "db.events"
},
{
"v" : 1,
"key" : {
"headers.for" : NumberLong(1)
},
"name" : "headers_for_1",
"ns" : "db.events"
}
]
Well the problem was the indexes I had.
{
"v" : 1,
"key" : {
"headers.occurredOn" : NumberLong(1),
"_id" : NumberLong(1)
},
"name" : "headers_occurredOn_1__id_1",
"ns" : "db.events"
}
After I dropped this one and added the following:
{
"v" : 1,
"key" : {
"headers.occurredOn" : NumberLong(1)
},
"name" : "headers.occurredOn_1",
"ns" : "db.events"
}
Everything went smooth

$Sum function in nested document MongoDB

I need the sum of ldl_xml_count for data array but I am unable to get the sum value for that field.
Design data
db.commination.findOne()
{
"_id" : ObjectId("5530dc5272b7e56b11497ce8"),
"data" : [
{
"ldl_date" : ISODate("2015-04-08T18:30:00Z")
},
{
"ldl_mmo_id" : 5
},
{
"ldl_xml_count" : 1
}
],
"masterid" : [
{
"ldl_date" : ISODate("2015-04-08T18:30:00Z")
},
{
"ldl_mmo_id" : 5
},
{
"ldl_xml_count" : 1
},
{
"ldl_master_info_id" : 11
}
],
"pubid" : [
{
"ldl_publication_info_id" : 41616,
"ldl_xml_info_id" : 37437691
}
],
"details" : [
{
"ldl_id" : 54261629,
"ldl_distribution_id" : 3289,
"ldl_local_flag" : 1,
"ldl_ftp_flag" : 0,
"ldl_time" : ISODate("2015-04-09T01:06:46Z")
}
]
}
mongo query
db.commination.aggregate([
{
$match:{
"data.ldl_date": {$gte: new Date("2015-01-08T18:30:00Z")}
}
},
{
$unwind :"$data"
},
{
$group: {
_id:"$data.ldl_mmo_id",
total: { $sum: "$data.ldl_xml_count" }
}
}
])
Output
{ "_id" : 1, "total" : 0 }
{ "_id" : 3, "total" : 0 }
{ "_id" : 4, "total" : 0 }
{ "_id" : 5, "total" : 0 }
{ "_id" : 2, "total" : 0 }
{ "_id" : null, "total" : 500000 }
I need the sum for id wise.

php parse json data from google directions

I'm trying to parse json data from google directions but can't seem to access access any of the values. Here is the php code;
$url = "http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles&sensor=false";
$jsonfile = json_encode(file_get_contents($url));
$jsondata = json_decode($jsonfile);
echo $jsondata->distance;
here is (some) of the json data returns:
{ "routes" : [ { "bounds" : { "northeast" : { "lat" : 41.90085000000001, "lng" : -87.62979000000001 }, "southwest" : { "lat" : 34.052360, "lng" : -118.243560 } }, "copyrights" : "Map data ©2012 Google", "legs" : [ { "distance" : { "text" : "2,015 mi", "value" : 3243390 }, "duration" : { "text" : "1 day 8 hours", "value" : 114318 }, "end_address" : "Los Angeles, CA, USA", "end_location" : { "lat" : 34.052360, "lng" : -118.243560 }, "start_address" : "Chicago, IL, USA", "start_location" : { "lat" : 41.87811000000001, "lng" : -87.62979000000001 }, "steps" : [ { "distance" : { "text" : "0.2 mi", "value" : 269 }, "duration" : { "text" : "1 min", "value" : 34 }, "end_location" : { "lat" : 41.87570, "lng" : -87.62969000000001 }, "html_instructions" : "Head \u003cb\u003esouth\u003c/b\u003e on \u003cb\u003eS Federal St\u003c/b\u003e toward \u003cb\u003eW Van Buren St\u003c/b\u003e", "polyline" : { "points" : "eir~FdezuOhFIF?HAdFG" }, "start_location" : { "lat" : 41.87811000000001, "lng" : -87.62979000000001 }, "travel_mode" : "DRIVING" }, { "distance" : { "text" : "0.2 mi", "value" : 328 }, "duration" : { "text" : "1 min", "value" : 51 }, "end_location" : { "lat" : 41.875680, "lng" : -87.63366000000002 }, "html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eW Congress Pkwy\u003c/b\u003e", "polyline" : { "points" : "czq~FpdzuO?jE#nEBfGAtB" }, "start_location" : { "lat" : 41.87570, "lng" : -87.62969000000001 }, "travel_mode" : "DRIVING" }, { "distance" : { "text" : "14.0 mi", "value" : 22564 }, "duration" : { "text" : "17 mins", "value" : 1031 }, "end_location" : { "lat" : 41.873870, "lng" : -87.903170 }, "html_instructions" : "Continue onto \u003cb\u003eI-290 W\u003c/b\u003e", "polyline" : { "points" : "_zq~Fj}zuOCtD?fA?f#?pC?P?T#V#zJ?##T?J?J?F?N?#?J?J?N?p##xA?h#?B?~A#xD?h#?lB?n##n#?n#?B#~G?f#A\\EvAGnBEz#EpA?JBlC?F?F?T?B?f#HtM?R?J?t#?|CCvDAb#E`EGlDKzCCd#OrBa#rFGfAAd#?HAp#?`ADbJDzJ?fB?dENdFB~N?d#?RFlH#~C?J#~A?H?XFrKAx##l#?n##^#bANnQ?rAAnMCnC#tH#x#FxJ?hB#tM?j#?p#BxFLrT?B?H?jQBbG#|AHxLBVJ~CLfC\\|EHtAHtAVpDf#tF\\dFTxCXhDVzBNtCFlEB|HBvEDlRAhC?xANxU#^?~A?nADxW#vA?l#Dv[?|#?\\#bCHdI#z#ZxDh#zDRx#T|#t#dCPf#bBlEf#dB^vA^jCR`CDv#?LHfBH`X?z#F|V#pF?`#L|Y#v##dKDbO?jEDdHZjHR`EBd#D`#h#~IHpB#p##n#?p#BjACvFE`IMrIQzIKfEc#tQEjCCn#}#dUSjFCf#mA~[Ar#QlMObGWlEEx#sB`_#Ep#aBt\\EdA?FcAbU?XCrCKnC?zBR|LB`#B\\?Dz#hG#F#Ft#vDJl#pBfMnAtHZ|ERrEJhTH|GLvJ^rHr#|HX`G#`FLnX#`B?`A#|CAbCRnT?BJ`OHrJDbC#r#XzZ?#DfHMdIe#bHIhD#zHPhP?##V#fBN|O?T#j#DnEHpQFlGDtCDtEDrEFlEt#jJF~#`#dDPtAXtCd#`IFlC#j#?j#?hAHpLB~C#bDA|DErBCl#Av#O`C]~DWzB[lB[fB]zAo#dCc#~Am#jBo#bBoCxG?#Sd#g#vAY~#St#W|#_#bBUhAYdB]vBMhA[vCKtAEbAIxA?PAXCb#AhA" }, "start_location" : { "lat" : 41.875680, "lng" : -87.63366000000002 }, "travel_mode" : "DRIVING" }, { "distance" : { "text" : "140 mi", "value" : 224957 }, "duration" : { "text" : "2 hours 13 mins", "value" : 8008 }, "end_location" : { "lat" : 41.53651000000001, "lng" : -90.32902000000001 }
The error returned is as follows:
( ! ) Notice: Trying to get property of non-object in C:\wamp2\www\phpAcademy\GoogleDirectionsApi\TMP25mnfz82oi.php on line 8
Call Stack
# Time Memory Function Location
1 0.0004 366752 {main}( ) ..\TMP25mnfz82oi.php:0
Google already returns JSON, so you do not need to encode it first. Secondly the 'distance' key is deeper in the response structure:
$origin = "Rome, Italy";
$destination = "Venice, Italy";
$key = "YOUR-OWN-KEY";
$url = "https://maps.googleapis.com/maps/api/directions/json?origin=".urlencode($origin).",IL&destination=" . urlencode( $destination) . "&sensor=false&key=" . $key;
$jsonfile = file_get_contents($url);
$jsondata = json_decode($jsonfile);
echo $jsondata->routes[0]->legs[0]->distance;
You can easily get all the data in json or array format:
$url = "https://maps.googleapis.com/maps/api/directions/json?origin=28.704059,77.102490&destination=30.719059,76.748704&sensor=false";
$jsonfile = file_get_contents($url);
$jsondata = json_decode($jsonfile);
echo $jsondata->routes[0]->legs[0]->distance;
if this error occur:
Catchable fatal error: Object of class stdClass could not be converted to string
than plz use for a output print_r() function

Categories