Need to fetch values from Angular JS array - php

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.

Related

How to recommend books for a user based on other users who bought and rate the books?

I am working on a simple recommendation engine using PHP and MongoDB. I am trying to recommend books for a user based on what other users bought. I have these data and I would like to count how many books each user has in common with the new user.
The collection fields are :
/* 1 */
{
"_id" : ObjectId("58474a61307ad40ea8003587"),
"userid" : "1",
"bookid" : "100",
"rate" : 4
}
/* 2 */
{
"_id" : ObjectId("58474aa9307ad40ea8003588"),
"userid" : "2",
"bookid" : "100",
"rate" : 4
}
/* 3 */
{
"_id" : ObjectId("58474ab0307ad40ea8003589"),
"userid" : "1",
"bookid" : "110",
"rate" : 4
}
/* 4 */
{
"_id" : ObjectId("58474ad7307ad40ea800358a"),
"userid" : "2",
"bookid" : "110",
"rate" : 3
}
/* 5 */
{
"_id" : ObjectId("58474adc307ad40ea800358b"),
"userid" : "3",
"bookid" : "100",
"rate" : 5
}
/* 6 */
{
"_id" : ObjectId("58474af4307ad40ea800358d"),
"userid" : "3",
"bookid" : "120",
"rate" : 4
}
/* 7 */
{
"_id" : ObjectId("58474b36307ad40ea8003592"),
"userid" : "2",
"bookid" : "120",
"rate" : 5
}
/* 8 */
{
"_id" : ObjectId("58474b43307ad40ea8003593"),
"userid" : "4",
"bookid" : "100",
"rate" : 2
}
I tried alot to find the solution but no luck. Any help appreciated :)
Edited:
I tried this code but it does not work
$out = $purchase->find(["userid"=>"1"]);
foreach($out as $a){
$pipeline = array(
array('$match' => array('bookid' => $a['bookid'])),
array('$group' => array('_id' => '$userid',
'count' => array('$sum' => 1)),
),
);
$result = $Ratingscollection->aggregate($pipeline);
foreach ($result as $aa){
echo $aa["_id"]."&nbsp";
echo $aa['count']."</br>";
}
As I understood your problem. You could try this. First store the bookid of the new user in a variable named as newUserBookId and suppose in our case it is 100.
Now Run the following Query-
> var newUserBookId ="100";
> db.xx.aggregate([{ $match: { "bookid": newUserBookId } },{$group : {"_id" : "$bookid", num_of_users: {$sum : 1}}}]);
Hoping this will help you.
Thanks

How to use $push in mongodb?

Hi all i am trying to push values to existing record using $push but i am getting error stating that
Invalid modifier specified: $push
I am using php below is my code php code
$collection->update(array('_id'=>3,"data._id"=>2),array('$push'=>array('userid','52')));
i.e adding 52 to userid. In 3 record and data._id 2
below is my table structure for mongo db
{ "_id" : 2,
"name" : "test",
"data" :[{"_id" : "1",
"file" : "nic",
"userid" : [1,2 ]
},
{"_id" : "2",
"file" : "nic1",
"userid" : [1 ]
},
{"_id" : 3,
"file" : "nick2",
"userid" : [1,2 ]
}
]},
{ "_id" : 3,
"name" : "test",
"data" : [{"_id" : "1",
"file" : "nic",
"userid" : [1,2 ]
},
{"_id" : "2",
"file" : "nic1",
"userid" : [3,2 ]
}
]}
Use the $ positional operator in your update that identifies an element in the data array to update without explicitly specifying the position of the element:
$collection -> update(
array('_id' => 3, "data._id" => 2),
array('$push' =>
array('data.$.userid' => 52)
)
);

Create a desired JSON format using data stored in PHP

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.

Retrieve elements using $slice

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.

How to search a string in inner array using mongodb?

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

Categories