Mongo make an array with date as key ?
[_id] => MongoId Object (
[$id] => 4fcf2f2313cfcd225700000d
)
[id] => 14
[name] => Aryan Roban
[news] => Array (
[08-06-2012] => 12
)
Here I want a make news as array with date as key and how to delete a particular key row ?
For example
I want to delete array element with key '08-06-2012' in news array , I dont know the value of it.
Finding Documents won't be a problem, this is very easy. Simply look if news has got a key which matches your search criteria:
db.foo.find({'news.08-06-2012': {'$exists': true}})
Don't forget to put an index on news.
But deleting them is not easily possible. There is another thread which show shows a way to do that, but it's really rather a workaround: In mongoDb, how do you remove an array element by its index Sadly this only works for arrays with numerical indexes and not for associative arrays.
Maybe you could use an own collection for news? Then you could update and delete them easily. Otherwise you could load a full document from your database, manipulate the news in your application and save it afterwards. This would require two datebase queries, but should work.
We can use like
db.foo.update({/*...*/}, {$unset: {'news.11-06-2012', 1}})
Related
I need to create a nested array using a recursive function. My sticking point is figuring out the correct key value when I insert something new.
The array would be empty initially and should look something like this after a few recursive calls.
[Family]
[0]
[Name]...
[1]
[Name]...
[Family]
[0]
[Name]...
[Family]
[0]
[Name]....
Each call to the recursive function creates one [Family] key along with its subsidiary [Name'] key (there are other keys of [Family] too, just keeping it to one for simplicity). That has to be inserted immediately after the last [Name] key in the array at whatever depth that happens to be at the time.
The input data is a GEDCOM file, a standard way of representing family keys in a simple text file format. My code to create a single [Family] key form the GEDCOM file works fine but I cannot figure out how to add each ['Family] key to the end of the the output array in each recursive call.
For example, after the first call to the recursive function, the array should look like this:
[Family]
[0]
[Name]...
[1]
[Name]...
The [Family] Key created by the recursive function would look like this:
[Family]
[0]
[Name]...
It should be inserted into the array after [Family][1][Name].
The problem I have is that I do not know the structure of the array at the time I need to append each [Family] key since the array is updated on each call to the recursive function, and there is an unknown number of dimensions in the array.
Pete
I have two, big, 2 dimensional arrays (pulled from some xml data) one (A list) is ~1000 items containing 5 fields the other (B list) is dinamically between 10.000-12.000 items containing 5 fields.
My idea was to compare EACH id key of list A against EACH id key of list B and on "true" compose a new array of combined fields, or just fields from array A if no match.
I used nested foreach loops and ended up with millions of iterations taking long time to process. needless to say...not a solution.
The form of this two structures and my needed result reminded me straight away of a sql join.
The questions are:
1.) Should i try sql or nested foreach might not be the best php way?
2.) Will a relational query be much faster than the iterations?
EDIT:
I pull data only periodically from an xml file (in a separate process) which contains 10+ fields for each node. Than i store the 5 fields i need in a CSV file to later compare with table A that i pull out from a mysql database. basically much like catalog update of attributes with fresh feed.
I'm affraid the original idea of storing into CSV was an error and i should just save the feed updates into a database too.
EDIT 2
The array list B look like this
Array
(
[0] => Array
(
[code] => HTS541010A9E680
[name] => HDD Mobile HGST Travelstar 5K100 (2.5", 1TB, 8MB, SATA III-600)
[price] => 385.21
[avail] => 0
[retail] => asbis
)
...
...
while the A list is similar in all but the 'code' field which is the only one useful for comparison
Array
(
[0] => Array
(
[code] => ASD-HTS541010A
[name] => HDD Mobile HGST Travelstar 5K100 (2.5", 1TB, 8MB, SATA III-600)
[price] => 385.21
[avail] => 0
[retail] => asbis
)
As you can see each feed will have universal code BUT some different random data as prefix or suffix so in each loop i have to do a couple of operations on the string to stripos or compare it to feeds id for a match or close match.
Pseudo code:
$mylist = loadfromDB();
$whslist = loadfromCSV();
foreach ($mylist as $myl) {
foreach ($whslist as $whl){
if ((stripos(code_a,code_b) OR (code_b,code_a) !== false)){
...
}
elseif (stripos(substr(strstr(code_a,'-'),1),code_b) !== false) {
...
}
elseif (stripos( substr(code_a,0,-5);) == !false ){
...
}
}
}
Using SQL will be faster because most SQL engines are optimized for joins, and your method is a brute-force method. However, inserting all that data to MySQL tables is quite a heavy task, so it's still not the best solution.
I suggest you do the join in PHP - but use a smarter algorithm. Start by sorting the two arrays by the field you want to match. Iterate both sorted arrays together - use two iterators(or pointers or indices or whatever) - lets say a iterates over A and b over B. On each iteration of the loop, compare the comparison field of the elements pointed by the a and b. If a's is smaller - advance a. If b's is smaller - advance b. If a's is equal to b's - you have a match, which you should store in a new list, and then advance both a and b(assuming the relation is one-to-one - if it's one-to-many you only advance the many iterator, and if it's many-to-many you need a bit more complex solution).
I'm using FQL to query the Facebook Insights table and return the data to my server, where I plan to store it within a MySQL database.
I've found that all the Insights metrics return different data types for the value field. For example, something like application_installation_adds will return a value like this:
Array
(
[0] => stdClass Object
(
[metric] => application_installation_adds
[value] => 3
)
)
..while a metric like application_permission_views_top will return this if it has data:
Array
(
[0] => stdClass Object
(
[metric] => application_permission_views_top
[value] => stdClass Object
(
[permissions_impression_email] => 5
[permissions_impression_user_birthday] => 4
[permissions_impression_read_insights] => 4
)
)
)
..and this if it's empty:
Array
(
[0] => stdClass Object
(
[metric] => application_permission_views_top
[value] => Array
(
)
)
)
Given the different data types and values, I was wondering what the best way to store this data within my database would be.
I was thinking of setting up a table like this:
metric_name
metric_subname
value
and then using a process like this:
Get FQL result.
If it's an empty Array, do nothing (because there is no data).
If it's a single value, insert metric_name and value. Give the metric_subname a value of "Singlular" (so that I know it's just a one-value metric).
If it's a stdCLass Object, use a foreach loop to fill in the metric_subname column.
This should give me a table like so, which will allow me to query the data in an easy manner down the track: http://i.stack.imgur.com/0fekJ.png
Can anyone please provide feedback? Is this a good way to go about it, or are there better options?
Thanks in advance! :)
Do you know how many levels deep the hierarchy of metrics can go? If more than two, your solution doesn't scale.
Assume at some point depth will reach > 2 and design a generalized schema. One that comes to mind would be to keep two tables: metrics, and user_metrics. The metrics table would just contain the hierarchy of possible metrics, with one change: instead of name and subname, implement id, name, parent_id. The second table would associate a metric with a user, and store the value, so: metric_id, user_id, value.
I've searched quite a bit for this but nothing has seem to to come up which is strange because I think it would be something that's something quite easy to implement. Basically I have a list of items which the user selects, what I want to do is update the values in the database according to the checkboxes selected basically the following scenarios:
The id assigned in the checkbox array is already in the DB and nothing needs to happen, or can be updated with the same value.
The id assigned in the checkbox array needs to be added to the database.
The id ISN't assigned in the checkbox array and therefor must be deleted from the DB.
has anyone got any code they worked with that does this?
EDIT:
the table is simple with two things being updated:
id_text and id_product
array coming from $_POST is:
Array ( [text] =>
Array ( [0] => 8 [1] => 1 [2] => 2 ) // the values that need to be inserted, updated, or deleted if they don't exist here.
[textValue] => ALL OF THEM
[id] => 33 // the 'id_product value'
[update] => update )
True, without any code or database squema is pretty hard to help, but if I understand correctly you can just:
Delete all values on DB.
Loop through the ids passed in POST or GET and insert them on DB.
Do an replace for every id passed. Replace behaves like insert i the entry does not exist and like update if it exists. I can not give you more details becuase I do not know your table structure
Delete all items that have a id that is not in your array. you can select those with "in":
...WHERE id_product NOT IN (1,3,5,8) AND......
you can easily generate this by using
$idCondition = 'id_product NOT IN (' . implode(',',$yourArrayName) . ')';
About REPLACE:
http://dev.mysql.com/doc/refman/5.1/de/replace.html
About IN:
http://www.webdevelopersnotes.com/tutorials/sql/tutorial_mysql_in_and_between.php3
EDIT: I just realized that some people might have objections on my deinition of REPLACE. What REPLACE actually does is deleting the row if it already exists and then do an INSERT.
what is the best way to get parent array key with multidimensional arrays?
for example I have this array:
array(
[0] => array(0=> sample, 1=>picture, 2=>frame, 3=>google)
[1] => array(0=> iphone, 1=>orange, 2=>love, 3=>msn)
[2] => array(0=> joe, 1=>geee, 2=>panda, 3=>yahoo)
)
now I need to search for example google and get the parent array key..
which it should be 0...any ideas? I used for loop for this but I think it will be slow if I have arrays with 700000 rows..
If you have an array with 700,000 rows you are almost certainly doing something wrong... I would first recomend thinking about utilizing a different data store: flat file or some type of DB.
foreach($array as $key => $value) {
if(in_array('google', $value)) return $key
}
Arrays with 700,000 rows? How many arrays? 9/10 times problem is that you've got your data set up wrongly.
I'm going to go ahead and assume you're doing a search of some sort. As you can't index an array (in the search meaning of index) then you're probably best putting the data into a database and making the most of column indexing to search fast.
Depending on context, you may alternatively want to think about storing your data in files, one per array, and using file searches to find which file contains your value.