I am trying to implement find() query in php. I have the following geoJSON :
{
"_id": {
"$oid": "dfgdg35g4334fwvwe34f"
},
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
12.12345678910111,
3.12345678910111
]
},
"properties": {
"name": "Any Name"
}
}
and querying as
$cursor = $collection->find(array("geometry"=> array("type"=>"Point")));
to get the results from geometry->type="Point" . What am i doing wrong cant seem to figure out the problem.
Once you get the cursor from the result of the query, you have to loop through the cursor ($cursor) in something like a foreach. Let mw know if you're able to solve the problem.
Related
To search an object in JSON from MYSQL using Laravel you do this:
$parent = DB::table($this->table)
->where("right_children->parent", $firstParent)
->get();
The problem now is how I can go about searching objects in arrays
[
{ "parent": "fish",
"children": { "right": "nunu"}
},
{ "parent": "cat",
"children": {"right": "nonha"}
}
]
IF I want to find the name "nunu" how to I search it using laravel?
I tried this:
DB::table($this->table)
->whereJsonContains("right_children->children->right", $firstParent)
->get();
but no result, I know I am getting it wrong
I am getting json array after getting applying query logic.
[
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-20"
},
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "CRP",
"booking_date": "2018-05-20"
},
{
"id": "4",
"diag_name": "Seepz Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-21"
}
]
But i want a more justified json array written below.
[
{
"diag_name": "LT Diagnostics",
"test_name": [
{
"id": "3",
"name" : "Alk PO4"
},
{
"id": "3",
"name" : "CRP"
}
],
"booking_date": "2018-05-20"
},
{
"diag_name": "Seepz Diagnostics",
"test_name": [
{
"id": "4",
"name" : "Alk PO4"
}
],
"booking_date": "2018-05-21"
},
]
I am not getting it,How to do in php. I want a more consolidate json format.
Have you tried changing your SQL query to group by diag_name and booking_date? That would be the first step I’d employ to get the outer data.
Formatting the data in the nested manner you’re after could be a function of whatever record serializer you’re using — does it support nested JSON as a return type, or only flat JSON as your example return value shows?
If the record set -> JSON serializer only ever returns flat data, the comments above are correct that you will have to write your own formatter to change the shape of the JSON yourself...
The accepted answer of this other question may be of help:
Create multi-level JSON with PHP and MySQL
I'm not a PHP guy but this is a typical scenario to use functional programming by means of the monad Map.
Looking online I've found this article that could help you.
Changing datasource output is not always (seldom indeed) a viable option.
Enjoy coding
{
"_id": NumberInt(13),
"name": "vishal",
"friends": [
{
"name1": "vini",
"count": NumberInt(213)
},
{
"name1": "sumesh",
"count": NumberInt(47)
}],
"blog": NumberInt(5)
}
i need to display the name,name1 of all doc & blog in php , using foreach() i got the parent document "name" but dint get name1& blog ?
you can try unwind in aggregation pipeline
db.collection.aggregate([{$unwind:"$friends"}])
Hope it will help
I have read about json_encode but still lack the logic in using it for my needs on this particular JSON structure.
Assuming the JSON structure is as follows :
{
"_id": "23441324",
"api_rev": "1.0",
"type": "router",
"hostname": "something",
"lat": -31.805412,
"lon": -64.424677,
"aliases": [
{
"type": "olsr",
"alias": "104.201.0.29"
}
],
"site": "roof town hall",
"community": "Freifunk/Berlin",
"attributes": {
"firmware": {
"name": "meshkit",
"url": "http:k.net/"
}
}
}
Some of the values of the attributes will be taken from the database while some are going to be hardcoded(static) like "type","api_rev". I was thinking of just using concatenation to build the structure but learnt its a bad idea. So if i am to use json_encode how may i be able to handle this structure ? array dimensions etc.
I've search Stack Overflow and Google for an answer but not luck. I'm trying to get the value of each of the locale with php in the following sample (facebook graph api). Any help would be appreciated.
"data": [
{
"id": "123456789/insights/page_fans_locale/lifetime",
"name": "page_fans_locale",
"period": "lifetime",
"values": [
{
"value": {
"en_US": 33975,
"fr_CA": 6906,
"fr_FR": 6105,
"en_GB": 5647
},
"end_time": "2012-03-14T07:00:00+0000"
},
{
"value": {
"en_US": 33992,
"fr_CA": 6906,
"fr_FR": 6107,
"en_GB": 5648
},
"end_time": "2012-03-15T07:00:00+0000"
},
}
First, this fragment is broken. data is a array not a JavaScript object.
Take the correct fragment and analyze data correctly. The best option is:
json_decode();
Here is how to use it.