I have two json files named: users.json and users_custom.json which I created from mysql database using php. users.json file looks like:
[{
"user_id" : "1",
"Name" : "Mr. A",
"phone" : "12345"
},
{
"user_id" : "2",
"Name" : "Mr. B",
"phone" : "23456"
}]
and users_custom.json file looks like:
[{
"user_id" : "1",
"Name" : "Mr. A Modified",
"email" : "someone#gmail.com"
},
{
"user_id" : "2",
"Name" : "Mr. B",
"address" : "some address"
}]
so, in users_custom.json file I have modified some fields and also added some new fields. Now, I want to merge users_custom.json over users.json file into users_final.json file. At the end users_final file should looks like this:
[{
"user_id" : "1",
"Name" : "Mr. A Modified",
"phone" : "12345"
"email" : "someone#gmail.com"
},
{
"user_id" : "2",
"Name" : "Mr. B",
"phone" : "23456"
"address" : "some address"
}]
At the end I will import the users_final.json file to MongoDB database. Any idea or example code will be greatly appreciated. Thanks in advance.
This should be fairly straightforward, get contents of both files, decode them both, loop them accordingly, if the user id's match, merge them, after that process is complete, encode the resultant, then write the file. Example:
// $contents_of_users = file_get_contents('users.json');
$contents_of_users = '[{ "user_id" : "1", "Name" : "Mr. A", "phone" : "12345"},{ "user_id" : "2", "Name" : "Mr. B", "phone" : "23456"}]';
// $contents_of_users_custom = file_get_contents('users_custom.json');
$contents_of_users_custom = '[{ "user_id" : "1", "Name" : "Mr. A Modified", "email" : "someone#gmail.com"},{ "user_id" : "2", "Name" : "Mr. B", "address" : "some address"}]';
$data_user = json_decode($contents_of_users, true);
$data_user_custom = json_decode($contents_of_users_custom, true);
$final = $data_user;
foreach($final as $key => &$user) {
foreach($data_user_custom as $user_custom) {
if($user['user_id'] == $user_custom['user_id']) {
$user = array_merge($user, $user_custom);
}
}
}
$final = json_encode($final, JSON_PRETTY_PRINT);
echo '<pre>';
print_r($final);
file_put_contents('users_final.json', $final);
Sample Output
Related
I have below scenario where, I have one collection "demo" having all data which need to be search,
I have used text search,
{
"_id" : "1",
"type" : "Deal",
"description_title" : "New Deal for test",
"images" : "",
"start_date" : ISODate("2017-07-20T00:00:00.000+0000"),
"end_date" : ISODate("2017-10-30T11:59:00.000+0000"),
"status" : "1"
}
{
"_id" : "2",
"type" : "Event",
"description_title" : "Event1 test",
"images" : "",
"end_date" : ISODate("2017-10-20T00:00:00.000+0000")
"status" : "1"
}
{
"_id" : "3",
"type" : "Text",
"description_title" : "test",
"images" : "",
"status" : "1"
}
{
"_id" : "4",
"type" : "Event",
"description_title" : "Event2 test",
"images" : "",
"end_date" : ISODate("2017-07-20T00:00:00.000+0000")
"status" : "1"
}
In above collection I have stored all data which need to be search by using text search, But when "type" : "Event"/"Deal" then it should only return Active Deals & Events "Current date is less than end_date" need to be searched.
In above collection When I search for "Test", It should return,
{
"_id" : "1",
"type" : "Deal",
"description_title" : "New Deal for test",
"images" : "",
"start_date" : ISODate("2017-07-20T00:00:00.000+0000"),
"end_date" : ISODate("2017-10-30T11:59:00.000+0000"),
"status" : "1"
}
{
"_id" : "2",
"type" : "Event",
"description_title" : "Event1 test",
"images" : "",
"end_date" : ISODate("2017-10-20T00:00:00.000+0000")
"status" : "1"
}
{
"_id" : "3",
"type" : "Text",
"description_title" : "test",
"images" : "",
"status" : "1"
}
I have used below query, but it gives me all result,
db.demo.find({'$text' : {'$search' => 'test'},'$or' : {
{"type" : "Deal","end_date" : {'$gte' : ISODate("2017-09-14T11:59:00.000+0000") }},
{"type" : "Text"},
{"type" : "Event","end_date" : {'$gte' : ISODate("2017-09-14T11:59:00.000+0000") }},
}})
Above query return all records.
Please help me to achieve my result.
How to update members age whose name is TEST1 using yii2.?
Used below code to update , but i am specifying the indexes there , i want with out specifying the indexes.
User::updateAll([ '$set'=> ['Addresses.0.members.0.age'=>100] ],['IN','Addresses.members.name',['TEST1'] ]);
{
"_id" : ObjectId("595209b65312f48195fb2e01"),
"username" : "Test name",
"Addresses" : [
{
"address_no" : 1,
"Address" : "Test house",
"City" : "test city",
"State" : "Test state",
"Mobile" : "9999999",
"members" : [
{
"name" : "TEST1",
"age" : 35
},
{
"name" : "TEST2",
"age" : 30
},
]
},
{
"address_no" : 2,
"Address" : "2B, Test place",
"City" : "Test city",
"State" : "Test State",
"Pincode" : "12345",
"Phone" : "1234568789",
"Mobile" : 9999999999
}
],
"Beneficiaries" : [
{
"beneficiary_id" : 1,
"Name" : "Test1",
"Age" : "28",
"Sex" : "F"
}
],
"auth_key" : "esd8d89ds89ds89ds89ds",
}
there is position operator $ to do this kind of job
{
"Addresses.members.name" : "TEST2",
},
{
$set: {
"Addresses.$.members.0.age" : 40
}
}
Here I specified first index as it supports up to one level depth.
New feature might release in future to resolve this issue: https://jira.mongodb.org/browse/SERVER-831
Yii::$app->mongodb->getCollection('user')->update(['_id' => $id, 'members.name' => 'Test1'], ['$set' => [
'members.$.age' => 100,
]]);
So I have a bunch of arrays inside which I have all the data I require to pass to a third party app. Problem is that they need it in a specific JSON format, and I do not have an idea how I can do that. The data format they require is like:
{
"appData" : {
"appKey" : "blah blah",
"synth" : {
"synth1" : {
"mono" : [
{
"monoId" : "529",
"templates" : [
{
"monoSequenceMap" : [
{
"map" : {
"X" : "3",
"Y" : "1"
},
"position" : {
"scale" : "1",
"x1" : "100",
"x2" : "150",
"y1" : "2000",
"y2" : "2500"
}
},
{
"map" : {
"X" : "2",
"Y" : "4"
},
"position" : {
"scale" : "1",
"x1" : "200",
"x2" : "550",
"y1" : "1000",
"y2" : "1500"
}
},
{
"map" : {
"X" : "3",
"Y" : "3"
},
"position" : {
"scale" : "1.5",
"x1" : "300",
"x2" : "750",
"y1" : "1750",
"y2" : "1800"
}
},
{
"map" : {
"X" : "4",
"Y" : "1"
},
"position" : {
"scale" : "1.5",
"x1" : "680",
"x2" : "790",
"y1" : "1950",
"y2" : "1850"
}
}
],
"templateId" : "01_A_19"
}
]
}
],
"synthId" : "XXXXXXXXXX"
}
}
}
}
I just want some pointers on how to convert the data I have into this JSON string. I think I need to use json_encode. Should I create a new class called 'appData' class then create each object/array inside it? or should I just write a string in this format into a text file?
My problem is that I cannot wrap my head around having all these objects inside objects thing...like for e.g, in the JSON synth is an object which contains synth1, synth2 etc which will be objects which in turn will have mono which will be an array of objects...And I am not sure how to tackle that..
Any pointers is greatly appreciated!
Are your arrays multidimensional? Like:
$array = array(
"data_table_1" => array(
"item1" => "Item 1",
"item2" => "Item 2"
),
"data_table_1" => array(
"item1" => "Item 1",
"item2" => "Item 2"
)
);
If so, all you have to use is use json_encode and that will do all the encoding for you:
$json = #json_encode($array);
==== Edit ====
arrays do not have to be multidimensional. Even an array with a single key => value will work. Just be sure you have keys for values, so they're registered correctly.
I have followed model stored in mongoDB:
{
"_id" : "some_table_name",
"content" : [{
"id" : "1",
"locname" : "KKH"
}, {
"id" : "2",
"locname" : "Singapore National Eye Centre"
}]
}
I try to find criteria to update 2nd element (id=2) aka add new String.
"new_element" : "foo"
So new view should be:
{
"_id" : "some_table_name",
"content" : [{
"id" : "1",
"locname" : "KKH"
}, {
"id" : "2",
"locname" : "Singapore National Eye Centre"
"new_element" : "foo"
}]
}
Form PHP
When I try to find 2nd node by id I use:
$array = $collection_bios2->findOne(
array("_id" => "some_table_name", "content.id" => "2"),
array("_id" => 0, "content.$" => 1)
);
But when I try to update it, new node enters under content:
$newdata = array('$set' => array("new_element" => "foo"));
$collection_bios2->update(
array("_id" => "some_table_name", "content.id" => "2"),
$newdata
);
I get:
{
"_id" : "some_table_name",
"content" : [{
"id" : "1",
"locname" : "KKH"
}, {
"id" : "2",
"locname" : "Singapore National Eye Centre"
}],
"new_element" : "foo"
}
Whats wrong in my implementation?
Please, help,
Maxim
You need to use the positional operator here:
array('$set'=>array('content.$.new_element':'foo'))
You can read more about it here: http://docs.mongodb.org/manual/reference/operator/positional/
I want to delete information from a document but the code runs and no error occurs.But it doesn't delete the record.
I haave data like that
{
"id": "12345",
"info": [
{
"sno":1
"name": "XYZ",
"email": "xyz#example.com"
},
{
"sno":2
"name": "XYZ",
"email": "xyz#example.com"
}
]
}
and I want to delete data where id=12345 and info.sno=2
my php code id
<?
$m=new Mongo();
$db=$m->database;
$cond=array("id"=>'12345');
$data=array('$pull'=>array("info.sno"=>2));
//I used before this $data=array('$pull'=>array("info"=>array("sno"=>2)));
echo json_encode($data);
$db->info->update($cond,$data);
$st=$db->Command(array("getlasterror"=>1));
?>
I run mongo db command like:
db.info.update({"id":12345},{'$pull':{"info":{"sno":2}}});
Your commented out line is correct:
test> db.foo.findOne()
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
}
]
}
test> db.foo.update({"id":"12345"}, {"$pull":{info:{sno:2}}})
test> db.foo.findOne()
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "XYZ",
"email" : "xyz#example.com"
}
]
}