how to display find() result of mongodb embedded document in php - php

{
"_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

Related

I want a more consolidate json format

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

Elasticsearch: What's the best way to search for a word within a string AND get score?

I'm using ElasticSearch's PHP client and I find really difficult to return results with scores whenever I want to search for a word that is "hidden" within a string.
This is an example:
I want to get all the documents where the field "file" has the word "anses" and files are named like this:
axx14anses19122015.zip
What I know about it
I know I should tokenize those words, can't realize how to do it.
Also I've read about aggregations but I'm really new to ES and I have to deliver a working piece ASAP.
What I've tried so far
REGEXP: using regular expressions is very expensive and does not return any scores, which is a must-to-have in order to shrink results and bring the user accurate information.
Wildcards: same thing, slow and no scores
Own script where I have a dictionary and search for critical words using regexp, if match, create a new field within that matched document with the word. The reason is to create a TOKEN so in future searches I can use regular match with scores. Negative side: the dictionary thing was totally denied by my boss so I'm here asking for any ideas.
Thanks in advance.
I suggest in your case nGram tokenizer see the example
I will create a analyzer and a mapping for a doc type
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"tokenizer": {
"ngram_tokenizer": {
"type": "nGram",
"min_gram": 4,
"max_gram": 4,
"token_chars": [ "letter", "digit" ]
}
},
"analyzer": {
"ngram_tokenizer_analyzer": {
"type": "custom",
"tokenizer": "ngram_tokenizer",
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"text_field": {
"type": "string",
"term_vector": "yes",
"analyzer": "ngram_tokenizer_analyzer"
}
}
}
}
}
after that I`ll insert a document using your file name
PUT /test_index/doc/1
{
"text_field": "axx14anses19122015"
}
now I`ll just will use a query match
POST /test_index/_search
{
"query": {
"match": {
"text_field": "anses"
}
}
}
and will receive a reponse like this
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.10848885,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.10848885,
"_source": {
"text_field": "axx14anses19122015"
}
}
]
}
}
What i did?
i just created a nGram tokenizer that will explode our string in 4 characters terms and will index this terms separated and they will be searched when I search a part of the string.
To see more, read this article https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch
Hope it help!
Ok after trying -so- many times it worked. I'll share the solution just in case someone else needs it. Thank you so much to Waldemar, it was a really good approach and I still cannot see why it's not working.
curl -XPUT 'http://ipaddresshere/tokentest' -d
'{ "settings":
{ "number_of_shards": 1, "analysis" :
{ "analyzer" : { "myngram" : { "tokenizer" : "mytokenizer" } },
"tokenizer" : { "mytokenizer" : {
"type" : "nGram",
"min_gram" : "3",
"max_gram" : "5",
"token_chars" : [ "letter", "digit" ] } } } },
"mappings":
{ "doc" :
{ "properties" :
{ "field" : {
"type" : "string",
"term_vector" : "yes",
"analyzer" : "myngram" } } } } }'
Sorry for bad indentation, I'm really hurry but want to post the solution.
So, this will take any string from "field" and split it into nGrams with lenght 3 to 5. For example: "abcanses14f.zip" will result in:
abc, abca, abcan, bca, bcan, bcans, etc... until it reaches anses or a similar term which is matcheable and has a score related to it.

find() query in php not working- Mongodb

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.

mongoDB remove sub-array

Ok is there a quick way to remove the follwoing though PHP mongodb
here is our mongoDB row
{
"today":""
"session": "6266262626",
"products": [
{
"barcode": "27788822",
"item": "village day ticket",
"price": 1315,
"qty": "3"
},
{
"barcode": "8544122",
"item": "village night ticket",
"price": 1433,
"qty": "1"
}
]
}
I would like to delete the product
{
"barcode": "8544122",
"item": "village night ticket",
"price": 1433,
"qty": "1"
}
I know how to update, and insert but cant figure out how to delete it.
here is a mongo command to delete the item, given its barcode :
db.collection.update({session:'6266262626'},{ $pull: { products: { barcode : '8544122' } }})
If you want to delete multiple items from the products array, given an array of barcodes :
db.collection.update({session:'6266262626'},{ $pull: { products: { barcode : {$in : ['27788822','8544122'] } } }})
I don't know the PHP equivalent of those commands, but here is a related question using $pull and PHP which may help :
MongoDB pull array element from a collection
this question is pretty old, but deleting multiple array items in a single query was giving me trouble today, and in my case the above is working out, so maybe it will help somebody else, thanks.
Reading the docs is your friend. Use the positional operator
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator

PHP parse facebook json

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.

Categories