DevSael Blog. C, CPP, C++, PHP, .NET Programming BLOG! advanced, MySQL, MongoDB, RDBMS.

Browsing "Older Posts"



Easiest way to do filtered results and pagination is as follows:
$cursor = $lib_collection->find($filter_params, array())
$count = $cursor->count();
$data = $cursor->skip(20)->limit(20);

However, this method may not be somewhat inefficient. If you query on fields that are not indexed, the only way for the server to "count()" is to load each document and check. If you do skip() and limit() with no sort() then the server just needs to find the first 20 matching documents, which is much less work. Remember that skip() and take() --no matter if you use an index or not-- will have to perform a scan. Therefore, skipping very far is very slow.
The number of results per category is going to be more difficult.

Think of it this way: The database has an index (B-Tree) that can compare values to each other: it can tell you quickly whether something is bigger or smaller than some given x. Hence, search times in well-balanced trees are logarithmic. This is not true for count-based indexation: A B-Tree has no way to tell you quickly what the 15.000th element is: it will have to walk and enumerate the entire tree.
If the data does not change often, you may want to precalculate these values using regular map/reduce jobs. Otherwise you have to run a series of distinct() commands or in-line map/reduce. Neither one is generally intended for ad-hoc queries.
The only other option is basically to load all of the search results and then count on the webserver (instead of the DB). Obviously, this is also inefficient.
Getting all of these features is going to require some planning and tradeoffs.

SQL is better optimized for "set-based" queries such as aggregation, where MongoDB is optimized for key-value lookups.One of the MongoDB "trade-offs" is that it is not good at real-time aggregation queries. Both styles of DB can do both things, but with different performance. Note, if you use $cursor->count() on something indexed, your response will be faster (true in all DBs) you may want to try this optimization.

MongoDB and PHP with pagination, distinct values (Performance issues)

By Game Changer → Saturday, December 5, 2015

If you are looking for a php algorithm to rank search results based on proximity/relevance of multiple words here comes a quick and easy way of generating search results with PHP only:
Issues with the other boolean search methods such as strpos()preg_match()strstr() or stristr()
  1. can't search for multiple words
  2. results are unranked
It sounds difficult but is surprisingly easy.
If we want to search for multiple words in a string the core problem is how we assign a weight to each one of them?
If we could weight the terms in a string based on how representative they are of the string as a whole, we could order our res
ults by the ones that best match the query.

On the performance side, use the strpos function. It is about three times faster and have in mind, when I did one million compares at once, it took preg match 1.5 seconds to finish and for strpos it took 0.5 seconds. What I'm trying to say is that it runs really fast either way.
if (strpos($a,'are') !== false) {
    echo 'true';
}
Note that the use of !== false is deliberate; strpos returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').

Another option is to use the strstr() function. Something like:

if (strlen(strstr($haystack,$needle))>0) {

// Needle Found

}
Point to note: The strstr() function is case-sensitive. For a case-insensitive search, use the stristr()function.

Not Using REGEXP:


function contains($text, $word) { $found = false; $spaceArray = explode(' ', $text); $nonBreakingSpaceArray = explode(chr(160), $text); if (in_array($word, $spaceArray) || in_array($word, $nonBreakingSpaceArray) ) { $found = true; } return $found; }
Make use of case-insensitve matching using stripos():
if (stripos($string,$stringToSearch) !== false) {
    echo 'true';
}
strstr() function is case-sensitive. For a case-insensitive search, use the stristr()function.

String can be checked with the below function

function either_String_existor_not($str,$character){
    if (strpos($str,$character) !== false) {
        return true;
    } else{
        return false;
    }
}
---end---

Best function to check if string contains specific words?

By Game Changer →

Hi Dude,
Actually there is no simple regular expression for this problem.  Confirmation email tokens are the only correct way to know you got the address of the person entering it. This is why most mailing lists now use that mechanism to confirm sign-ups. After all, anybody can put down president@whitehouse.gov, and that will even parse as legal, but it isn't likely to be the person at the other end.
This regular expression will only validate addresses that have had any comments stripped and replaced with whitespace (this is done by the module).
If you're looking for something simpler but that will catch most valid email addresses try something like:
"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
Remember, After all, the best way to validate the email address is still to actually send an email to the address in question to validate the address. If the email address is part of user authentication (register/login/etc), then you can perfectly combine it with the user activation system. I.e. send an email with a link with an unique activation key to the specified email address and only allow login when the user has activated the newly created account using the link in the email.
If the purpose of the regex is just to quickly inform the user in the UI that the specified email address doesn't look like in the right format, best is still to check if it matches basically the following regex:
^([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)$
Not to mention that non-Latin (Chinese, Arabic, Greek, Hebrew, Cyrillic and so on) domain names are to be allowed in the near future. Everyone has to change the email regex used, because those characters are surely not to be covered by [a-z]/i nor \w. They will all fail.

What's the best regular expression you have or have seen for validating emails?

By Game Changer →
Actually it is impossible to answer and has been brought up many many times before. It depends on whether you prefer a dynamic scripting language or a strongly-typed modular language. I'd suggest you start with PHP though as then you don't have to deal with more advanced Asp.net concepts like events, controls, viewstate, class libraries, e
tc. You can pick those things up easy enough once you've got to grips with the syntax and programming for the web.
ASP and PHP are similar in that both tend to put their code in with the HTML, and so the logic can be quite similar.
But, ASP.NET will be very different from PHP in design, as there is a strong incentive to use code-behind in ASP.NET, where you basically have the html template and all the code is in another separate file
Depending on what you are doing, how busy your site is, you may find that the speed difference is inconsequential, though one is compiled and the other isn't.
PHP is probably going to be faster to develop, as you can more easily code a little and test, than you can with ASP.NET, but ASP and PHP are similar in how you can develop.
If you don't know any of these languages then PHP may be the easier one to learn, as the php manual is so well written, with lots of comments from users, and ASP.NET has replaced ASP, so learning ASP for a new project, IMO, is of limited use.
If you go with ASP.NET then you are learning a new syntax and one of the .NET languages, but depending on your background, C# may be relatively easy to learn.
With ASP or ASP.NET you are stuck with using IIS for your server, but with PHP you can use IIS or Apache, so there is considerable flexibility there.
With ASP.NET you will find more options to help with code development, as they now have the classic ASP.NET and ASP.NET MVC (http://www.asp.net/%28S%28d35rmemuuono1wvm1gsp2n45%29%29/mvc/), both with pros and cons, but I believe this site is still written in the latter.
So, which would be better depends on what you are going to be doing with it, and what languages or frameworks you have already gained experience with.

ASP.net is quite cheap - you can use free ide like visual studio web developer express edition - the only thing you lose is things like source control and some other features available from professional edition onwards.
ASP.NET can do threads, while PHP cannot. Honestly that's about it. Someone will come and nit-pick about some other complicated task that PHP can't do, but PHP is a pretty robust and dynamic language overall.
If you are starting now and have never done C# or VB development, I would do PHP instead. It's much easier to pick up and has far fewer rules compared to C#. Yes, it can lead to bad coding practices because it is so loose and open. However, the documentation is phenomenal and you'll be moving much more quickly than you would in ASP.NET with no C# or VB experience.
Frankly speaking, once you are into professional development, the benefit of going for VS professional edition far outweighs the costs associated with it.
Apart from the tooling costs you have to consider the following costs and benefits
  1. Windows server - though you can run this on mono, I rarely see people choosing ASP.net for the specific case of running on mono. Windows server and IIS is a far better option to run ASP.net and the cost is justified due to easier management of Windows
  2. Cost of training - this is only if your current team is not trained in ASP.net
  3. Cost of development - here you actually stand to gain a little because the tooling for .net platform is by far the best. You will see productivity of even average developers improve a lot and good developers too can benefit from all kinds of features. Debugging capabilities, advanced intellisense, and overall better integration with other tools like VSTS make this a worthwhile investment.
  4. SQL Server - in case you decide to go for the paid editions, then there is a cost associated - again its not necessary, for most applications, an express edition might be quite sufficient. I must say that there are many features I have gotten used to in SQL server that are not present in MySQL and Postgresql. Merge replication is one of them, but there are others as well. Do your own research to see if the cost is worthwhile for your application.
PHP will run on essentially any server, for free. That's a fairly compelling feature for many folks.
There are lots of pros and cons of both, and it certainly doesn't boil down to scripting vs. compiled (incidentally, opcode caches like APC and things like Facebook's HipHop even the score on that point).
I'd say if someone's recommending PHP over ASP.NET, they code primarily in PHP. If they're recommending ASP.NET over PHP, they code primarily in ASP.NET. There's probably not much more to it than that in the responses you're getting.

ASP and PHP are similar in that both tend to put their code in with the HTML, and so the logic can be quite similar.
But, ASP.NET will be very different from PHP in design, as there is a strong incentive to use code-behind in ASP.NET, where you basically have the html template and all the code is in another separate file
Depending on what you are doing, how busy your site is, you may find that the speed difference is inconsequential, though one is compiled and the other isn't.
PHP is probably going to be faster to develop, as you can more easily code a little and test, than you can with ASP.NET, but ASP and PHP are similar in how you can develop.
If you don't know any of these languages then PHP may be the easier one to learn, as the php manual is so well written, with lots of comments from users, and ASP.NET has replaced ASP, so learning ASP for a new project, IMO, is of limited use.
If you go with ASP.NET then you are learning a new syntax and one of the .NET languages, but depending on your background, C# may be relatively easy to learn.
With ASP or ASP.NET you are stuck with using IIS for your server, but with PHP you can use IIS or Apache, so there is considerable flexibility there.
With ASP.NET you will find more options to help with code development, as they now have the classic ASP.NET and ASP.NET MVC (http://www.asp.net/%28S%28d35rmemuuono1wvm1gsp2n45%29%29/mvc/), both with pros and cons, but I believe this site is still written in the latter.
So, which would be better depends on what you are going to be doing with it, and what languages or frameworks you have already gained experience with.

ASP.NET OR PHP?

By Game Changer →