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.
Related
I have an object of json type column in my table (properties) in MySQL like:
[
{
"unit": "2",
"floor": "1",
"price": "6000000",
"toilet": "2",
"balcony": "2",
"bedrooms": "2",
"customer": "3",
"bathrooms": "3",
"flat_name": "1A",
"flat_size": "1200",
"floor_plan": "217",
"price_per_sqft": "5000"
},
{
"unit": "2",
"floor": "1",
"price": "5000000",
"toilet": "2",
"balcony": "2",
"bedrooms": "2",
"customer": null,
"bathrooms": "3",
"flat_name": "1B",
"flat_size": "1200",
"floor_plan": "215",
"price_per_sqft": "5000"
},
{
"unit": "1",
"floor": "2",
"price": "6000000",
"toilet": "2",
"balcony": "2",
"bedrooms": "2",
"customer": null,
"bathrooms": "3",
"flat_name": "2A",
"flat_size": "1250",
"floor_plan": "216",
"price_per_sqft": "5300"
}
]
How can I update customer id, where flat_name = 1B from this object in Laravel?
I have made the solution with using loop. Thank you who reply and answer
$objectitem=Property::where("id", 1)->first();
$updatedFlatDetails= $objectitem->flat_details;
foreach ($objectitem->flat_details as $key => $singleflat){
if($singleflat['flat_name']==$item->flat_name){
$updatedFlatDetails[$key]['customer']=(string)$item->customer_id;
}
}
$objectitem->flat_details = $updatedFlatDetails;
$objectitem->save();
Use collection after fetch data from database :
$target= $collection->filter(function ($item) {
return $item->flat_name=="1B";
})->values();
or filter before fetch data use eloquent :
Model::where('flat_name','1B')->get();
I'm running a query in mongo using php like this:
$column ='address.roadname';
(new MongoDB\Client())->db->mycollection->distinct($column,[],['$sort'=> [$column => 1]]);
The query gives results, but those are not sorted.
What am I missing?
The data are just strings "nested" into inner object
What I expect is a list of street names sorted alphabetically
sample data:
[
{
"name": "tizio",
"address": [
{
"roadType": "via",
"roadname": "Roma",
"number": "12 bis",
"city": "Milano"
},
{
"roadType": "via",
"roadname": "Emilia",
"number": "124",
"city": "Modena"
},
{
"roadType": "via",
"roadname": "Appia",
"number": "89",
"city": "Genova"
}
]
},
{
"name": "caio",
"address": [
{
"roadType": "vicolo",
"roadname": "stretto",
"number": "12",
"town": "Monza"
},
{
"roadType": "largo",
"roadname": "Garibaldi",
"number": "24",
"city": "Modena"
},
{
"roadType": "piazza",
"roadname": "Armi",
"number": "26",
"city": "Rovigo"
}
]
},
{
"name": "sempronio",
"address": [
{
"roadname": "Roma",
"number": "15",
"city": "Milano"
},
{
"roadType": "via",
"roadname": "Po",
"number": "4",
"city": "Torino"
},
{
"roadType": "largo",
"roadname": "Garibaldi",
"number": "9",
"community": "Genova"
}
]
}
]
what I expect:
Appia,Armi,Emilia,Garibaldi,Po,Roma,Stretto
note: if I run it on mongo console
db.mycollection.distinct("address.roadname").sort()
I got the expected result
The PHP implementation of distinct does have a sort option, see https://docs.mongodb.com/php-library/v1.7/reference/method/MongoDBCollection-distinct/
In your shell example,
db.mycollection.distinct("address.roadname")
returns an array, so when you run .sort() on that it is using the javascript Array.sort method.
The PHP distinct function also returns an array, so use the sort function on it.
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've been struggling around this for a while now and can't seem to get the right algorithm.
What I want is to sort this:
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
And the output would be
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
},{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}, {
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}],
"position": "0"
}
}';
So basically it should be sorted using the label inside the "options" without affecting the other fields. I've been trying usort and tried different algorithms but to no avail.
Current code that sorts only the outside "label" and not the inner label:
function optionsSort($a, $b) {
return strcmp($a['options'][0]['label'], $b['options'][0]['options']['label']);
}
usort($variable, "optionsSort"));
Here's the code:
// your array
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
$a = json_decode($variable);
foreach ($a as $item) {
// sorting function
usort($item->options, function($a, $b) { return strcmp($a->label, $b->label); });
}
I have a json tree with categories,
I would like to obtain the last level of elements for each different category.
For example on this json:
[
{
"category_id": "3",
"parent_id": "2",
"name": "Women",
"categories": [
{
"category_id": "11",
"parent_id": "3",
"name": "Clothing",
"categories": [
{
"category_id": "30",
"parent_id": "11",
"name": "T-shirts",
"categories": null
},
{
"category_id": "33",
"parent_id": "11",
"name": "jeans",
"categories": null
}
]
}
]
},
{
"category_id": "5",
"parent_id": "2",
"name": "Footwear ",
"categories": [
{
"category_id": "15",
"parent_id": "5",
"name": "Rings",
"categories": [
{
"category_id": "51",
"parent_id": "15",
"name": "Small Leathers",
"categories": null
}
]
},
{
"category_id": "16",
"parent_id": "5",
"name": "Bands",
"categories": [
{
"category_id": "41",
"parent_id": "16",
"name": "boots",
"categories": null
}
]
},
{
"category_id": "48",
"parent_id": "5",
"name": "Bracelets",
"categories": [
{
"category_id": "55",
"parent_id": "48",
"name": "Cocktail",
"categories": null
}
]
}
]
}
]
The result would be an array (T-shirts, Jeans, Small Leathers, boots, cocktail)
What I was thinking is to decode it on an array and search filter the array with all the categories that are null, but I'm not sure if it's the best option because the object have different levels.
(I'm sorry for the English)
Json string is not a valid. It can be made valid by enclosing the json string in {}.
$json = '{"categories": [
{
"category_id": "3",
"parent_id": "2",
"name": "Women",
"categories": [
{
"category_id": "11",
"parent_id": "3",
"name": "Clothing",
"categories": [
{
"category_id": "30",
"parent_id": "11",
"name": "T-shirts",
"categories": null
},
{
"category_id": "33",
"parent_id": "11",
"name": "jeans",
"categories": null
}
]
}
]
},
{
"category_id": "5",
"parent_id": "2",
"name": "Footwear ",
"categories": [
{
"category_id": "15",
"parent_id": "5",
"name": "Rings",
"categories": [
{
"category_id": "51",
"parent_id": "15",
"name": "Small Leathers",
"categories": null
}
]
},
{
"category_id": "16",
"parent_id": "5",
"name": "Bands",
"categories": [
{
"category_id": "41",
"parent_id": "16",
"name": "boots",
"categories": null
}
]
},
{
"category_id": "48",
"parent_id": "5",
"name": "Bracelets",
"categories": [
{
"category_id": "55",
"parent_id": "48",
"name": "Cocktail",
"categories": null
}
]
}
]
}
]}';
Decode the json string in to php opject
$json_object = json_decode($json);
Use this recursive function to fetch get all the last level categories in a array.
function getLastCategories($object){
$last_categories = array();
if(is_array($object->categories)){
foreach($object->categories as $categories){
$last_categories = array_merge( $last_categories, getLastCategories($categories));
}
}else{
$last_categories[]=$object->name;
}
return $last_categories;
}
print_r(getLastCategories($json_object));
Output:
Array
(
[0] => T-shirts
[1] => jeans
[2] => Small Leathers
[3] => boots
[4] => Cocktail
)
You can do this using recursive iterators.
$categories = json_decode($json);
// create the iterator
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($categories));
// iterate recursively
foreach ($iterator as $key => $subcategories) {
// if the 'categories' key is empty for the current level...
if ($key == 'categories' && !$subcategories) {
// then add the value of the 'name' key for the current level to your result array.
$result[] = $iterator->getSubIterator()->offsetGet('name');
}
}
This may not be the most efficient way to do it, but it makes the code pretty simple.