Delete a specific data from mongo - php

I want to delete specific data from mongo. below is my json. I want to delete all occurence of IIT-395.
{
"_id" : "i34908s",
"lifeStageCourses" : [
{
"lifeStage" : "in_school",
"tags" : [
"IIT-182"
],
"courseIds" : []
},
{
"lifeStage" : "in_college",
"tags" : [
"IIT-134",
"IIT-140",
"IIT-395"
],
"courseIds" : []
},
{
"lifeStage" : "prep_entrance_exam",
"tags" : [],
"courseIds" : []
},
{
"lifeStage" : "job_seeker",
"tags" : [
"IIT-134",
"IIT-395"
],
"courseIds" : []
}
]
}
I expect output only IIT-395 should be removed
Instead whole document is getting deleted.

try following query .
db.getCollection('careerpaths').update(
{"lifeStageCourses.tags" : "IIT-395"},
{$pull : {
"lifeStageCourses.$[].tags" :"IIT-395"
}},false, true
);

Related

Reading data from an array with json

In a php script I have the following
$jsonurlpers = "https://api.openarch.nl/1.1/records/show.json?archive=nha&identifier=8554bfba-9fd9-4ca2-876c-feb7da095d6c";
$jsondatapers = file_get_contents($jsonurlpers);
That string I want to decode with the next command
$jsonpers = json_decode($jsondatapers ,true)[0];
But somewhere it did not work...
When I go to to the #jsonulpers I see :
Event
EventType "Overlijden"
EventDate
Year "1901"
Month "7"
Day "7"
EventPlace
Place "Egmond-Binnen"
RelationEP
Can some one tell me how I can read the value behind EventType in a variable? I tried different options but none works...
Thanks,
Fred
I did try this
e.g.
$itemevent = $jsonpers['Event'];
$itemeventtype = $itemevent['EventType']);
abs I thought
$itemeventtype would have a value of "Overlijden", but it was empty
The (json-)output from the url you gave looks like this:
{
"Event" : {
"EventType" : "Overlijden",
"EventDate" : {
"Month" : "7",
"Day" : "7",
"Year" : "1901"
},
"EventPlace" : {
"Place" : "Egmond-Binnen"
}
},
"RelationEP" : [
{
"RelationType" : "Overledene",
"EventKeyRef" : "Event1",
"PersonKeyRef" : "Person1"
},
{
"PersonKeyRef" : "Person2",
"RelationType" : "Vader",
"EventKeyRef" : "Event1"
},
{
"PersonKeyRef" : "Person3",
"RelationType" : "Moeder",
"EventKeyRef" : "Event1"
}
],
"Source" : {
"SourcePlace" : {
"Place" : "Egmond-Binnen / Egmond-Binnen"
},
"SourceAvailableScans" : {
"Scan" : {
"Uri" : "https://nha.blob.core.windows.net/scans/BS%20Overlijden/Egmond-Binnen/1901/RNH_O_EGB_1901_006-a.jpg",
"OrderSequenceNumber" : "1"
}
},
"SourceLastChangeDate" : "2015-07-31",
"SourceRemark" : {
"Value" : "Datadump ExportBS+Overlijden_20210302_102010.csv van NHA via e-mail"
},
"SourceReference" : {
"InstitutionName" : "Noord-Hollands Archief",
"DocumentNumber" : "14",
"Place" : "Haarlem"
},
"RecordGUID" : "{8554bfba-9fd9-4ca2-876c-feb7da095d6c}",
"SourceIndexDate" : {
"To" : "1901-12-31",
"From" : "1901-01-01"
},
"SourceDate" : {
"Year" : "1901",
"Month" : "7",
"Day" : "8"
},
"SourceType" : "BS Overlijden"
},
"Person" : [
{
"BirthPlace" : {
"Place" : "Egmond-Binnen"
},
"PersonName" : {
"PersonNameLastName" : "Baltus",
"PersonNameFirstName" : "Aafje"
},
"Gender" : "Vrouw",
"Age" : {
"PersonAgeYears" : "8 maanden"
}
},
{
"PersonName" : {
"PersonNameFirstName" : "Jan",
"PersonNameLastName" : "Baltus"
}
},
{
"PersonName" : {
"PersonNameLastName" : "Kuijper",
"PersonNameFirstName" : "Grietje"
}
}
]
}
After "Person":, you see a [, which means there is an array of Persons.
The first person does have a "BirthPlace", for the other persons it seems to be unknown what the "BirthPlace" is, because it is not mentioned in the json.
print($jsonpers["Person"][0]["PersonName"]["PersonFirstName"] should be: "Aafje"
and
print($jsonpers["Person"][2]["PersonName"]["PersonFirstName"] should be: "Grietje"

Laravel 5 read Json file give error `Response.php line 403`

I want to read json file from database directory, but give The Response content must be a string or object implementing __toString(), "boolean" given. error. Bellow is my code :
$json = file_get_contents('database/query_templates/course.json');
$data = json_decode($json, TRUE);
echo '<pre/>'; print_r($data); exit;
course.json file content :
{
"post_filter" : {
"operator" : [
{
"operator" : [
{
"range" : {
"created_at" : {
"gte" : "log_start_date",
"lte" : "log_end_date",
"format" : "yyyy-MM-dd"
}
}
},
{
"terms" : {
"search_param_user_action.id" : "user_select_array"
}
}
]
},
{
"operator" : [
{
"operator" : [
"conduct_days_multi"
]
},
{
"operator" : [
"dropdowns"
]
}
]
},
{
"query" : {
"multi_match" : {
"query" : "x",
"fields" : [
"resource_search_columns"
],
"operator" : "operator"
}
}
}
]
}
}
for any help thanks.
Sometimes some php releasees not supported get_file_contents() or this func return type string but not only return string everytime, sometimes return boolean value bacause it's working on the byte codes, you can try traditional file operation methods or you can try convert to string.

Parse json with PHP. Getting Undefined property: stdClass errors

I am having some issues parsing a json file from Jenkins using PHP
{
"actions" : [
{
"causes" : [
{
"shortDescription" : "Started by an SCM change"
}
]
},
{
},
{
},
{
"buildsByBranchName" : {
"origin/release_5.6.0" : {
"buildNumber" : 242,
"buildResult" : null,
"marked" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
},
"revision" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
}
},
"origin/release_5.7.0" : {
"buildNumber" : 315,
"buildResult" : null,
"marked" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
},
"revision" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
}
},
I have tried doing the following
//Read in JSON object
$json_file2 = file_get_contents('url.com/json');
//Decode JSON file
$test = json_decode($json_file2); //object
//print_r($json_file2);
echo $test->causes;
I am also trying to access the different sections in "buildsByBranchName". I have tried many different variations of the code above, but I keep getting "Undefined property: stdClass" errors.
You are not accessing that value properly. causes resides under actions which is an array. Your code also won't work because causes is an array.
// This is an array so you can't use echo here.
$causes = $test->actions[0]->causes;
// echo out the shortDescription
echo $causes[0]->shortDescription;
or
echo $test->actions[0]->causes[0]->shortDescription;

Find a document with Doctrine ODM with equals condition on nested array of objects

I got this kind of document:
{
"_id" : ObjectId("54ad5c3b9a703a3c088b4567"),
"hard" : 750,
"coordinates" : {
"x" : 0.2388169910939489,
"y" : 0.7996551291084174
},
"indicator" : 500,
"networkIdList" : {
"networkIdData" : [
{
"networkId" : "abc123",
"type" : "SomeNetwork"
},
{
"networkId" : "123asdf",
"type" : "AnotherNetWork"
},
{
"networkId" : "abc123",
"type" : "OneMoreNetwork"
}
]
}
}
And I need to perform a query to find the document that have "networkId" = "abc123" AND "type" = "SomeNetwork".
I have tried With this instruction:
$this->documentManager->createQueryBuilder('Mydocument') ->field('networkIdList.networkIdData.$.networkGamingId')->equals('abc123') ->field('networkIdList.networkIdData.$.type')->equals('')
->getQuery()
->execute());
But the cursor return no data.
I also try with
->where("function() {return this.networkIdList.networkIdData.$.networkGamingId == 'abc123'}")
but in this case i got an error that says the Object $ has no propierties.
And I need to perform a query to find the document that have "networkId" = "abc123" AND "type" = "SomeNetwork"
$qb = $dm->createQueryBuilder('Foo')
->field('networkIdList.networkIdData.networkId')->equals('abc123')
->field('networkIdList.networkIdData.type')->equals('SomeNetwork');

Parsing this json file using PHP

I have the following json file. I need code in php to parse it. I tried all possible ways but no luck. I am not an expert in json parsing.
{
"_id" : { "oid" : "5213785fe4b0780ba56884d3" },
"author" : "Remate.ph",
"message" : "Suspected ASG abducts trader in Zamboanga City http://t.co/4hUttNPI",
"time_created" : 1357958119000,
"version" : "v2.1",
},
{
"_id" : { "oid" : "5213785fe4b0780ba56884d6" },
"author" : "Clydepatrick Jayme ",
"message" : "RT #iFootballPlanet: 74' Osasuna 0 - 0 Real Madrid\n\n-ASG.",
"time_created" : 1358022721000,
"version" : "v2.1",
}
I modified the above json file in the following way. And I wrote the php parsing code for it and it works fine.
{
"info1":{
"_id" : { "oid" : "5213785fe4b0780ba56884d3" },
"author" : "Remate.ph",
"message" : "Suspected ASG abducts trader in Zamboanga City http://t.co/4hUttNPI",
"time_created" : 1357958119000,
"version" : "v2.1",
},
"info2":{
"_id" : { "oid" : "5213785fe4b0780ba56884d6" },
"author" : "Clydepatrick Jayme ",
"message" : "RT #iFootballPlanet: 74' Osasuna 0 - 0 Real Madrid\n\n-ASG.",
"time_created" : 1358022721000,
"version" : "v2.1",
}
}
I used the following code to parse it.
<?php
//$string=file_get_contents("/Users/Anirudh/Downloads/test.json");
$string=file_get_contents("/Users/Anirudh/Sem-1/RA/Test/test.json");
$json_a=json_decode($string,true);
$jsonIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json_a),RecursiveIteratorIterator::SELF_FIRST);
//$jsonIterator = new RecursiveArrayIterator(json_decode($string, TRUE));
foreach ($jsonIterator as $key => $val) {
echo "$key => $val\n";
}
?>
Can someone help me in getting the first json format parsed using PHP ?
Your json file has two errors:
Two 1st level items separated by comma (info1 and info2) must be an array, wrap the whole string with brackets [ and ].
Last 2nd level items (version) - remove trailing commas "version" : "v2.1",.
Corrected json:
[{
"_id" : { "oid" : "5213785fe4b0780ba56884d3" },
"author" : "Remate.ph",
"message" : "Suspected ASG abducts trader in Zamboanga City http://t.co/4hUttNPI",
"time_created" : 1357958119000,
"version" : "v2.1"
},
{
"_id" : { "oid" : "5213785fe4b0780ba56884d6" },
"author" : "Clydepatrick Jayme ",
"message" : "RT #iFootballPlanet: 74' Osasuna 0 - 0 Real Madrid\n\n-ASG.",
"time_created" : 1358022721000,
"version" : "v2.1"
}]

Categories