My result is following after executing find() query.
{
"_id" : ObjectId("5384928a03ea2e75268b4567"),
"0" : {
"name" : "mango",
"quantity" : "10"
},
"1" : {
"name" : "apple",
"quantity" : "14"
},
"2" : {
"name" : "banana",
"quantity" : "11"
},
"3" : {
"name" : "grapes",
"quantity" : "19"
},
"4" : {
"name" : "lichi",
"quantity" : "13"
},
"5" : {
"name" : "orange",
"quantity" : "10"
},
"6" : {
"name" : "lemon",
"quantity" : "10"
},
"7" : {
"name" : "pear",
"quantity" : "10"
},
"8" : {
"name" : "cherry",
"quantity" : "10"
},
"9" : {
"name" : "kiwi",
"quantity" : "10"
}
}
Now i want only any five elements in result(like from 2nd element to 6th element).
How to do this using $slice or is there any other method to retrieve only five elements in result?
The important thing here is, do you actually have an array for $slice to work on? There seems to be a bit of a misconception here common to people working with PHP as the way that language typically represents an array.
The mongo shell ( for example ) will show the clear distinction, where an array actually looks like this in it's JSON representation:
{
"_id" : ObjectId("5384928a03ea2e75268b4567"),
"arrayField": [
{
"name" : "mango",
"quantity" : "10"
},
{
"name" : "apple",
"quantity" : "14"
},
{
"name" : "banana",
"quantity" : "11"
},
{
"name" : "grapes",
"quantity" : "19"
}
]
}
That structure can use the $slice operator to return the elements you want. As an example here, 2 documents from index position 1
db.collection.find({},{ "arrayField": { "$slice": [ 1,2] }})
{
"_id" : ObjectId("5384928a03ea2e75268b4567"),
"arrayField" : [
{ "name" : "apple", "quantity" : "14" },
{ "name" : "banana", "quantity" : "11" }
]
}
The structure you are showing is just a bunch of key names for sub-documents within the top level document. It is probably a mistake but you just specify those fields in the projection. It's not an array so $slice does not apply:
db.collection.find({}, { "1": 1, "2": 1, "3": 1, "4": 1, "5": 1, "6": 1 })
But that probably is not what you want, so it looks like you will need to fix your data so it is an array.
$slice is used to control the number of elements in an array returned by the query, but I don't see an explicit array in your results. Your document should look something like:
{
"_id" : ObjectId("5384928a03ea2e75268b4567"),
"fruit" : [
{
"name" : "mango",
"quantity" : "10"
},
{
"name" : "apple",
"quantity" : "14"
}
]
}
Then you could query it with something like:
db.collection.find({},{"fruit": {$slice:[1:6]}})
If you want to get only 5 record from document you can use limit(), if you want to skip few of the record then use skip().
For Example :
db.collection.find().skip(2).limit(5);
This query fetch 5 documents after first 2 documents.
Related
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,
]]);
I am using a MVC model in PHP. I am getting the following string from the View layer (This is a angular.js array but I am getting it as a string):
[
{
"name" : "item",
"price" : "123",
"quantity" : 12,
"id" : 1
}, {
"name" : "hhh",
"price" : "000",
"quantity" : 12,
"id" : 2
}, {
"name" : "kk",
"price" : "88",
"quantity" : 12,
"id" : 3
}
]
How can I extract the values of name, price, quantity and id from this string and put that into insert query?
This is what is known as a serialized array, meaning that it is a JavaScript array in string form (JSON). You can use PHP's json_decode function to deserialize the string, from there you can use it as a normal array:
$json='[
{
"name" : "item",
"price" : "123",
"quantity" : 12,
"id" : 1
}, {
"name" : "hhh",
"price" : "000",
"quantity" : 12,
"id" : 2
}, {
"name" : "kk",
"price" : "88",
"quantity" : 12,
"id" : 3
}
]';
$array=json_decode($json);
foreach ($array as &$value) {
var_export($value->name);
var_export($value->price);
var_export($value->quantity);
var_export($value->id);
}
The above should display all the values in your array. I'm not sure what you mean by "put that into the insert query", but hopefully the above will help you get access to this data.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create a json object as follows from php. How could I do it. I'm using MySQL table to get data.
{
"JSChart" : {
"datasets" : [
{
"type" : "line",
"id" : "blue",
"data" : [
{
"unit" : "1",
"value" : "80"
},
{
"unit" : "2",
"value" : "40"
},
{
"unit" : "3",
"value" : "60"
},
{
"unit" : "4",
"value" : "65"
},
{
"unit" : "5",
"value" : "50"
},
{
"unit" : "6",
"value" : "50"
},
{
"unit" : "7",
"value" : "60"
},
{
"unit" : "8",
"value" : "80"
},
{
"unit" : "9",
"value" : "150"
},
{
"unit" : "10",
"value" : "100"
}
]
},
{
"type" : "line",
"id" : "green",
"data" : [
{
"unit" : "1",
"value" : "100"
},
{
"unit" : "2",
"value" : "55"
},
{
"unit" : "3",
"value" : "80"
},
{
"unit" : "4",
"value" : "115"
},
{
"unit" : "5",
"value" : "80"
},
{
"unit" : "6",
"value" : "70"
},
{
"unit" : "7",
"value" : "30"
},
{
"unit" : "8",
"value" : "130"
},
{
"unit" : "9",
"value" : "160"
},
{
"unit" : "10",
"value" : "170"
}
]
},
{
"type" : "line",
"id" : "gray",
"data" : [
{
"unit" : "1",
"value" : "150"
},
{
"unit" : "2",
"value" : "25"
},
{
"unit" : "3",
"value" : "100"
},
{
"unit" : "4",
"value" : "80"
},
{
"unit" : "5",
"value" : "20"
},
{
"unit" : "6",
"value" : "65"
},
{
"unit" : "7",
"value" : "0"
},
{
"unit" : "8",
"value" : "155"
},
{
"unit" : "9",
"value" : "190"
},
{
"unit" : "10",
"value" : "200"
}
]
}
],
"optionset" : [
{
"set" : "setSize",
"value" : "550, 300"
},
{
"set" : "setAxisValuesNumberY",
"value" : "5"
},
{
"set" : "setIntervalStartY",
"value" : "0"
},
{
"set" : "setIntervalEndY",
"value" : "200"
},
{
"set" : "setLabelX",
"value" : "[2,'p1']"
},
{
"set" : "setLabelX",
"value" : "[4,'p2']"
},
{
"set" : "setLabelX",
"value" : "[6,'p3']"
},
{
"set" : "setLabelX",
"value" : "[8,'p4']"
},
{
"set" : "setLabelX",
"value" : "[10,'p5']"
},
{
"set" : "setAxisValuesNumberX",
"value" : "5"
},
{
"set" : "setShowXValues",
"value" : "false"
},
{
"set" : "setTitleColor",
"value" : "'#454545'"
},
{
"set" : "setAxisValuesColor",
"value" : "'#454545'"
},
{
"set" : "setLineColor",
"value" : "'#A4D314', 'green'"
},
{
"set" : "setLineColor",
"value" : "'#BBBBBB', 'gray'"
},
{
"set" : "setTooltip",
"value" : "[1,' ']"
},
{
"set" : "setTooltip",
"value" : "[2,' ']"
},
{
"set" : "setTooltip",
"value" : "[3,' ']"
},
{
"set" : "setTooltip",
"value" : "[4,' ']"
},
{
"set" : "setTooltip",
"value" : "[5,' ']"
},
{
"set" : "setTooltip",
"value" : "[6,' ']"
},
{
"set" : "setTooltip",
"value" : "[7,' ']"
},
{
"set" : "setTooltip",
"value" : "[8,' ']"
},
{
"set" : "setTooltip",
"value" : "[9,' ']"
},
{
"set" : "setTooltip",
"value" : "[10,' ']"
},
{
"set" : "setFlagColor",
"value" : "'#9D16FC'"
},
{
"set" : "setFlagRadius",
"value" : "4"
},
{
"set" : "setAxisPaddingRight",
"value" : "100"
},
{
"set" : "setLegendShow",
"value" : "true"
},
{
"set" : "setLegendPosition",
"value" : "490, 80"
},
{
"set" : "setLegendForLine",
"value" : "'blue', 'Click me'"
},
{
"set" : "setLegendForLine",
"value" : "'green', 'Click me'"
},
{
"set" : "setLegendForLine",
"value" : "'gray', 'Click me'"
}
]
}
}
Can any one help me to do that. If there any tutorials to follow. I want to use JSChart to plot data on my HTML.
Your way is to use json_encode function:
return json_encode(array('JSChart' => array(
'datasets' => array(
...
),
));
You should try something like below code to populate your json with PHP and MySQL:
$info=array();
while($row = mysql_fetch_array($res,MYSQL_ASSOC)){
array_push($info,$row);
}
echo json_encode($info);
would return:
array(2) { [0]=> array(3) { ["id"]=> string(1) "1" ["firstname"]=> string(3) "foo" ["lastname"]=> string(3) "bar" } [1]=> array(3) { ["id"]=> string(1) "2" ["firstname"]=> string(3) "foo" ["lastname"]=> string(3) "bar" } }
json:
[{"id":"1","firstname":"foo","lastname":"bar"},{"id":"2","firstname":"foo","lastname":"bar"}]
How to search value in multidimensional array,
for example I want to search example keyword in the following data in mongodb
I used to fetch all data from command
>db.info.find()
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 3,
"name" : "XYZ",
"email" : "xyz#demo.com"
},
{
"sno" : 4,
"name" : "ABC",
"email" : "abc#demo.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
Now, to find data having example I used command
>db.info.find({"info.email":"example"})
and it gives
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 3,
"name" : "XYZ",
"email" : "xyz#demo.com"
},
{
"sno" : 4,
"name" : "ABC",
"email" : "abc#demo.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
But I want only 3 out of 5 sub rows like
{
"_id" : ObjectId("4f74737cc3a51043d26f4b90"),
"id" : "12345",
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
Rohan, MongoDB always returns the whole document that you are searching on. You can't just make it return the array elements in which your keyword was found. If you want to do that, then you need to make sure all all embedded documents in the "info" field are in their own collection. And that might mean that you need to link them back to the original document in your "info" collection. Perhaps something like:
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
"info_id" : "12345",
},
Alternatively, you can of course do post-processing in PHP to obtain only the rows that you want.
Perhaps this is a good idea?
http://php.net/manual/en/class.mongoregex.php
I tried Map Reduce Function and it works on this type of problems the code is something like that:
Write a map function
map=function ()
{
filter = [];
this.info.forEach(function (s) {if (/example/.test(s.email)) {filter.push(s);}});
emit(this._id, {info:filter});
}
Write a reduce function
reduce=function(key, values) { return values;}
MapReduce Function
res=db.info.mapReduce(map,reduce,{out:{inline:1}})
And The Output look likes:
"results" : [
{
"_id" : ObjectId("4f9a2de0ea4a65c3ab85a9d3"),
"value" : {
"info" : [
{
"sno" : 1,
"name" : "ABC",
"email" : "abc#example.com"
},
{
"sno" : 2,
"name" : "XYZ",
"email" : "xyz#example.com"
},
{
"sno" : 5,
"name" : "Rohan",
"email" : "rohan#example.com"
}
]
}
}
],
"timeMillis" : 1,
"counts" : {
"input" : 3,
"emit" : 3,
"reduce" : 0,
"output" : 3
},
"ok" : 1,
Now you can find your search data from
printjson(res.results)
Did you try $ (projection)?
db.info.find({"info.email":"example"}, {"info.email.$":1})
document