Mongo Db insert element into nested mongo array - php

I have following document structure
{
assets : {
type : [
{
type : spread
scene : [
{
....
},
{
....
}
]
}
]
}
}
So I want to add new element in "scene" array with match condition of type "spread" in same document
How can I write Mongo Update query for the same ?

Following is example array
{
assets : {
type : [
{
type : spread
scene : [
{
....
},
{
....
}
]
}
]
}
}
Reference link
MongoDB & Meteor - Query to push into nested array does not work, no error thrown
And query which insert new element in scene array is
db.PhotostoryDesigner.update(
{
"photostoryId":"123",
"assets.type.type":"spreads"
},
{"$push": {
'assets.type.$.scene':{"$each":[
{
"scene_id": "1",
"name": "Ad hoc 1",
"spread": [
{
"spreadId": "7725",
"name": "PAGE 1-2",
"asset": []
}
]
}
]}}
}
);

Related

How to add item to an object in php

I have a nested object which is generated by a base data in a function. the data generated is several levels deep (data_tree)
To enter each children I use a recursive function.
What I want is to add the data of the children2 item to children and remove children2
Next I show the data_tree in JSON format to get an idea of ​​how the structure is. remember that the data_tree is an object
{
"data_tree": [
{
"code":"PE",
"country":"Peruvian",
"language" :"spanish",
"children":[
{
"code": "DEP1" ,
"region":"Tumbes",
"children": [
{
"code": "PRO1",
"province":"Contralmirante Villar"
}
],
"children2": [
{
"code": "PRO2",
"province":"Zarumilla "
}
]
},
{
"code": "DEP2" ,
"region":"Piura",
"children": [],
"children2": [
{
"code": "PRO4",
"province" :"Paita"
},
{
"code": "PRO5",
"province":"Sullana"
}
]
},
{
"code": "DEP3" ,
"region":"Lambayeque"
}
],
"children2":[
{
"code": "DEP4" ,
"region":"La Libertad"
},
{
"code": "DEP5" ,
"region":"Lima"
}
]
},
{
"code":"EC"
"country":"Ecuador",
"language" :"spanish",
"children":[
{
"code": "4" ,
"province":"Quito"
},
{
"code": "5" ,
"province":"Guayaquil"
}
],
"children2":[]
}
]
}
the recursive function i use is: if it has children it keeps iterating.
$traverse = function ($data_tree) use (&$traverse) {
foreach ($data_tree as &$node) {
if (empty($node->children)) { //if the children is empty
unset($node->children); //I remove the children to add it with the data of children2
$node->children =$node->children2;
unset($node->children2);//I delete children2
}else{
//in case I have data, how do I add the data from dechildren2 to children?
}
$traverse($node->children);
}
};
$traverse($data_tree);
return $data_tree;
All problem solved using json decode mean in associative array
$data_json='[{"children":[],"children2":[{"children":[],"children2":[{"code":"PRO1","children":[],"children2":[{"code":"PRO1"}]}]}]},{"children":[],"children2":[{"children":[{"code":"PRO1","children":[],"children2":[{"code":"PRO1"}]}]}]}]';
//$data_tree=;
$data_tree =json_decode($data_json,true);
// var_dump($data_tree);
$traverse = function (&$data_tree) use (&$traverse) {
foreach ($data_tree as &$node) {
// var_dump($node);
if (empty($node['children']) && !empty($node['children2'])) { //if the children is empty
// var_dump($node);
$node['children'] =$node['children2'];
unset($node['children2']);//I delete children2
}else{
//in case I have data, how do I add the data from dechildren2 to children?
}
if(isset($node['children'])){
$traverse($node['children']);
}
if(isset($node['children2'])){
$traverse($node['children2']);
}
}
};
$traverse($data_tree);
Output
[{"children":[{"children":[{"code":"PRO1","children":[{"code":"PRO1"}]}]}]},{"children":[{"children":[{"code":"PRO1","children":[{"code":"PRO1"}]}]}]}]

Yii2's mongodb collection - get all content of nested arrays

I have a Yii2 mongodb's collection that looks like this:
{
"_id" : "123",
"books" : [
{
"author" : "John",
"title" : "The Book"
},
{
"author" : "Smith",
"title" : "Something!"
}
]
}
{
"_id" : "321",
"books" : [
{
"author" : "John",
"title" : "The Book"
}
]
}
...
And I want to get an array of all books (an array of arrays basically):
[
{
"author" : "John",
"title" : "The Book"
},
{
"author" : "Smith",
"title" : "Something!"
},
{
"author" : "John",
"title" : "The Book"
}
...
]
I saw answers to close questions, but they all achieve something a bit different.
Also tried $collection->distinct('books', [], []) and it worked, but it removed duplicates which is unacceptable.
Use this MongoDB query to get this resultset
db.collection.aggregate([
{ $unwind : "$books"},
{ $group: {
_id: null,
items:
{ $push: "$books" }
}}
]);
Let's try like this way using foreach()?
<?php
$json = '[{"_id":"123","books":[{"author":"John","title":"The Book"},{"author":"Smith","title":"Something!"}]},{"_id":"321","books":[{"author":"John","title":"The Book"}]}]';
$array = json_decode($json, 1);
$ids = [];
foreach($array as $v) {
foreach($v['books'] as $book){
$books[] = $book;
}
}
echo json_encode($books);
?>
Output:
[{"author":"John","title":"The Book"},{"author":"Smith","title":"Something!"},{"author":"John","title":"The Book"}]
DEMO: https://3v4l.org/hdJ8H

Finding List of Key Names in MongoDB Query or with PHP

{
"Name": {
"Year": {
"Model": {
"SubModel": {
"Category": {
"SubCategory": [
{
"firstline": "Value1",
"secondline": "Value2"
},
{
"firstline": "Value3",
"secondline": "Value4"
},
{
"firstline": "Value5",
"secondline": "Value6"
}
]
}
}
}
}
}
},
.....
.....
I have a JSON data like this and I can insert this data in MongoDB.
Now can i get the list of "Name", list of "Year" under a "Name",
list of "Model" under a "Year", list of "SubModel" under a "Model", list of "Category" under a "Submodel" and so on?
In MongoDb you can access you documents just as you access a JSON object. Suppose you want get the list of subcategory of a certain collection. Try like this:
db.getCollection('NewCollection').findOne({_id:'some Object Id'})['Name']['Year']['Model']['SubModel']['Category']['SubCategory']
with the help of #rashik-hasnat, I want to add these to list the key names only:
var data = db.getCollection('NewCollection').findOne({_id:'some Object Id'})['Name']['Year']['Model']['SubModel']['Category']['SubCategory'];
for (var key in data){
print(key);
}

elasticsearch query by array of id result returned is not sorted by the array of id pass in

I am using elastica and below is my query :
$query = new Query();
$query->setSize(5);
$qb = new \Elastica\Query\Ids();
$qb->addId("id_5");
$qb->addId("id_3");
$qb->addId("id_4");
$qb->addId("id_1");
$qb->addId("id_2");
return $query->setQuery($qb)
I want the result return in the same order as what I passed in for example in this case it will be "id_5, id_3, id_4, id_1, id_2"
however what i get is the sorting is not the same as what I wanted
I see this as two solution
1. if you have id as part of document then you can do
POST indexName/_search
{
"sort": [
{
"_script": {
"type": "number",
"script": "sortOrder.indexOf(doc['Id'].value)",
"params": {
"sortOrder": [
"AVMX9sHTyNVr4SjF3oRt",
"AVMTc1fSyNVr4SjF3WpF",
"AVMYuGLOyNVr4SjF3rKm"
]
},
"order": "asc"
}
}
],
"query": {
"ids": {
"values": [
"AVMX9sHTyNVr4SjF3oRt",
"AVMTc1fSyNVr4SjF3WpF",
"AVMYuGLOyNVr4SjF3rKm"
]
}
}
}
If not then you need to update mapping for your type to be
{
"mappings": {
"YOUR_TYPE": {
"_id": {
"index": "not_analyzed"
}
}
}
}
Then you can use "script": "sortOrder.indexOf(doc['_id'].value)"

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;

Categories