I have a document like this in mongodb
{
"_id": {
"$oid": "56b7451fa91b80e2078b4567"
},
"number": "222",
"value": "hello",
"name": "James",
"username": "mahsa",
"lessons": [
{
"id": "2",
"time": "2016-02-11 07:15:00",
"term": "3"
"marks": [
{
"id": "4",
"value": "10",
},
{
"id": "5",
"value": "9",
}
]
},
{
"id": "3",
"time": "2016-02-11 07:45:00",
"term": "4"
}
]
}
I'd like to push a nested array only when my variables are exactly this: array('number'=>'222','lessons.id'=>'2','lessons.term'=>'3')
after
"id": "2", "time": "2016-02-11 07:15:00", "term": "3"
, but when I use
array('number'=>'222','lessons.id'=>'2','lessons.term'=>'4')
or
array('number'=>'222','lessons.id'=>'3','lessons.term'=>'3')
has a similar result
my code is
$this->collection->update(
array('number'=>'222','lessons.id'=>'2','lessons.term'=>'3'),
array('$push'=>array(
'lessons.$.marks'=> array(
array('id'=>'444','value'=>'100'),
array('id'=>'555','value'=>'90')
))));
Can I solve this problem or mongo can't support somethings like this?
Mongo does support it, you just need to use the proper syntax:
{
'number': '222',
'lessons': {
'$elemMatch': {
'id' : '3',
'term': '3'
}
}
}, {
'$push': {
'lessons.$.marks': {
'$each': [{
'id': '444',
'value': '100'
}, {
'id': '555',
'value': '90'
}]
}
}
}
Here is probably what you need in PHP:
$this->collection->update(
array('number'=>'222',
array('$elemMatch'=>array('lessons.id'=>'2','lessons.term'=>'3')),
array('$push'=>array(
'lessons.$.marks'=> array(
'$each'=>array(
array('id'=>'444','value'=>'100'),
array('id'=>'555','value'=>'90')
)))));
Related
My MongoDB document looks something like this -
{
"VENDOR_ID": "101",
"NAME": "Test",
"DELETEFLAG": "1",
"DATETIME_MODIFIED": "2021-04-22 05:31:27am",
"CONTACT": [{
"CONTACT_ID": "111",
"CONTACT_TYPE": "CONTACT",
"DELETEFLAG": "1",
"LID": "197",
"USER_ID": "101",
"VALUE": "222222222222"
}, {
"CONTACT_ID": "222",
"CONTACT_TYPE": "EMAIL",
"DELETEFLAG": "1",
"LID": "197",
"USER_ID": "101",
"VALUE": "Test#gmail.com"
}, {
"CONTACT_ID": "333",
"CONTACT_TYPE": "CONTACT",
"DELETEFLAG": "2",
"LID": "197",
"USER_ID": "101",
"VALUE": "444444444444"
}, {
"CONTACT_ID": "444",
"CONTACT_TYPE": "CONTACT",
"DELETEFLAG": "2",
"LID": "197",
"USER_ID": "101",
"VALUE": "888888888888"
}]
}
I want to fetch all the Contacts for which the **DELETEFLAG** is set to 1. My collection can have multiple vendor documents.
I'm using PHP and fetching all the VENDORS with DELETEFLAG set to 1 along with their contacts. My query looks something like this -
$filterproduct = array(
"CONTACT.DELETEFLAG"=>'1',
"DELETEFLAG" => '1',
"LID"=>$LID,
);
$resultproduct1 = $this->GetMany($collectionName,$filterproduct,$fetchArr);
If I understand, you maybe want this:
db.collection.aggregate([
{ $unwind: "$CONTACT" },
{
$match: {
"CONTACT.DELETEFLAG": "1",
},
},
]);
Yopu break all documents in contacts with $unwind and so, get only new generated documents with CONTACT.DELETEFLAG = 1 with $match.
In my JsonArray result, I want to put all the content in index [{'data':{contenthere}}]
Here is my code :
$data = [];
$gr = []; //some data here
foreach($gr as $g) {
$data[] = [
'id' => $g['id'],
'name' => $g['name'],
'phone' => $g['pĥone']
]
}
return $data;
return $data output :
string(249) "[
{
"id": "112",
"name": "john",
"phone": "XXXXXXXXX"
},
{
"id": "213",
"name": "mike",
"phone": "XXXXXXXXX"
},
{
"id": "246",
"name": "jess",
"phone": "XXXXXXXXX"
},
]
cool, now I want to put all this in ['data'] so the result needed :
string(249) "[
{
"data": {
{
"id": "112",
"name": "john",
"phone": "XXXXXXXXX"
},
{
"id": "213",
"name": "mike",
"phone": "XXXXXXXXX"
},
{
"id": "246",
"name": "jess",
"phone": "XXXXXXXXX"
},
}
}
]
As Google JSON Style
I tried the $data[]['data'] but the "data": repeats in each object
foreach($gr as $g) {
$data[]['data'] = ...
I thought do do group_by function but I think that there is a simple solution for that
I'm not sure if you really need the extra array at the top level, but use...
return [["data" => $data]];
should give you the result.
Use json_encode to achieve this:
$json = json_encode(array('data' => $data));
print_r($json);
Result:
{
"data": [
{
"id": "112",
"name": "john",
"phone": "XXXXXXXXX"
},
{
"id": "213",
"name": "mike",
"phone": "XXXXXXXXX"
},
{
"id": "246",
"name": "jess",
"phone": "XXXXXXXXX"
}
]
}
I have a list of (the result of a query in DB) like this:
[
{
"id": "1",
"parent_id": null,
"title": "مدادنوکی",
"url": "/medadnoki"
},
{
"id": "2",
"parent_id": null,
"title": "جامعه",
"url": "/commiunity"
},
{
"id": "5",
"parent_id": "1",
"title": "درباره ی مدادنوکی",
"url": "/about"
},
{
"id": "6",
"parent_id": "1",
"title": "درباره ی مدادنوکی",
"url": "/about"
},
{
"id": "7",
"parent_id": "2",
"title": "همکاران",
"url": "/co-worker"
},
{
"id": "8",
"parent_id": "2",
"title": "اساتید",
"url": "/masters"
}
]
But I want to create an object like this:
[
{
"title": "مدادنوکی",
"url": "/medadnoki",
"subs" : [
{
"title": "درباره مدادنوکی",
"url": "/about"
},
{
"title": "درباره مدادنوکی",
"url": "/about"
},
{
"title": "درباره مدادنوکی",
"url": "/about"
},
{
"title": "درباره مدادنوکی",
"url": "/about"
}
]
},
{
"title": "جامعه",
"url": "/soc",
"subs" : [
{
"title": "همکاران",
"url": "/co-work"
},
{
"title": "اساتید",
"url": "/masters"
}
]
}
]
I have handled this process with two foreach in php and it means I process data twice and it is not efficient and will be slow.
Is there any Idea to doing this with just one foreach?
using one foreach means faster than two foreach twice and It will show the result in big data
Assuming you have the results in an order where the parents appear in the results before any children they may have, this should work:
$nested = [];
foreach($results as $r) {
if($r['parent_id'] === null) {
$nested[$r['id']] = [
'title' => $r['title'],
'url' => $r['url'],
'subs' => []
];
continue;
}
$nested[$r['parent_id']]['subs'][] = [
'title' => $r['title'],
'url' => $r['url']
];
}
$nested = array_values($nested);
I want JSON object as follows in that personal, address and itm have sequence of json object.
{
"id": "1",
"state": "12",
"personal": [
{
"name": "abc",
"contact":"1111111"
"address": [
{
"line1": "abc",
"city": "abc",
"itm": [
{
"num": 1,
"itm_detatils": {
"itemname": "bag",
"rate": 1000,
"discount": 0,
}
}
],
"status": "Y"
}
]
}
]
}
But I am getting result as follows in that I want json array at address and itm_details.
{
"id": "1",
"state": "12",
"personal": [
{
"name": "abc",
"contact": "1111111",
"address": {
"line1": "abc",
"city": "abc",
"itm": {
"inum": "1",
"itm_detatils": {
"itemname": "bag",
"rate": 1000,
"discount": 0
}
},
"status": "Y"
}
}
]
}
My PHP Code is as follow:
In that I am creating simple array and after that array inside array but during encoding to json it's not showing sequence of json object.
$a=array();
$a["id"]="1";
$a["state"]="12";
$a["personal"]=array();
$a["personal"][]=array(
"name"=>"abc",
"contact"=>"1111111",
"address"=>array(
"line1"=>"abc",
"city"=>"abc",
"itm"=>array(
"inum"=>"1",
"itm_detatils"=>array(
"itemname"=>"bag",
"rate"=>1000,
"discount"=>0,
),
),
"status"=>"Y",
),
);
echo json_encode($a);
Thanks in advance.
Add one more array
//...
"address" => array(
array(
"line1"=>"abc",
"city"=>"abc",
// ...
),
)
Below is my JSON file:
{
"example": "1",
"example2": 2,
"text": "3",
"info": {
"agent": 4,
"sum": 5,
"collection": [{
"Name": "6",
"Pic": "7"
} {
"Name": "8",
"Pic": "9"
}, {
"Name": "10",
"Pic": "11"
}]
}
}
How would I display each 'name' and 'pic' I think I need to use a foreach loop but don't know how to.
This is all the code I have:
$data = json_decode(file_get_contents('http://linktojson.com'));
echo $data['info']['collection'][0]['Name'];
echo $data['info']['collection'][0]['Pic'];
This should work
$data = json_decode(file_get_contents('http://linktojson.com'));
echo "<pre>".print_r($data,1)."</pre>";
foreach($data->info->collection as $key){
echo $key->Pic;
echo $key->Name;
}
Valid JSON
{
"example": "1",
"example2": 2,
"text": "3",
"info": {
"agent": 4,
"sum": 5,
"collection": [{
"Name": "6",
"Pic": "7"
}, {
"Name": "8",
"Pic": "9"
}, {
"Name": "10",
"Pic": "11"
}]
}
}