Mongod error | exception in initAndListen std::exception: locale::facet::_S_create_c_locale name not valid

I got this error while trying to start mongodb on amazon ec2. Running this command in the terminal solved the problem.

export LC_ALL=C

I don’t have any idea about what it means at this time except that it sets the terminal to display all languages. If you can explain what actually it does, please drop a comment.

Posted in MongoDB, OSX, PYTHON | Tagged , , | Leave a comment

Right way to break Django code for debugguing

Django traceback is a very helpful tool in Django development. It saves us from the boring tasking of writing print statements to track variables.

Traceback doesn’t kick when there is no error.  There may be no coding errors but there is a flaw in our application logic which we want to debug. In such cases we can trigger Django traceback by adding assert False in our code.

example:

def hello_world(response):
test_var = "Something"
test_var2 = "another var"
assert false
return HttpResponse(test_var+test_var2)

Posted in Django, PYTHON | Tagged , , , | Leave a comment

Where are python site packages located in MAC OSX Mountain Lion

Python site packages are located at

Library/Python/2.7/site-packages

Posted in PYTHON | Tagged | Leave a comment

Get the number of documents in a MongoDB collection with unique/distinct values for a field

Some times we need to get the number of documents with a distinct/unique value. An example use case is in the classic blog example.

  • Get the number of authors with at least one post.
  • Get the number of distinct tags/categories

You can use the following commands to get the answers for the above questions

db.posts.distinct('author').length
db.posts.distinct('tag').length

Reference:

Posted in CodeSnippets, MongoDB | Tagged | Leave a comment

A complete guide to Integrating MongoDB with Elastic Search

After almost two weeks and several re-installs and fresh installs, I finally got to integrate mongodb and elastic search. Here is a step by step procedure on how to integrate them.

If you follow this procedure carefully,  it will prevent errors like

Exception: java.lang.NoSuchMethodError: com.mongodb.Mongo.fsyncAndLock()

{
“error” : “IndexMissingException[[testmongo] missing]”,
“status” : 404
}

Follow these guide to install MonogDB and Elastic Search

After you installed them, its time to install elastic search river.

Download the snapshot from here. Extract it and copy its contents to elastic_search_root/plugins/plugins/mongodb_river

Note: The initial implementation tutorial give on the git page points to a older version of the snapshot and it doesn’t work with the latest versions of elastic search and mongodb.

Installing the mongodb river

Run the following two commands to install the mongodb river. If you are on a slow connection, the first command can take more than 15 minutes.

ES_HOME/bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/1.4.0 
ES_HOME/bin/plugin -install richardwilly98/elasticsearch-river-mongodb/1.4.0 

After you install both of them, restart elasticsearch.

ES_HOME/bin/service/elasticsearch restart

Enable replica sets in mongodb by following this tutorial

Tell elastic search to index the “person” colletion in testmongo database by issuing the following command in your terminal

curl -XPUT 'http://localhost:9200/_river/mongodb/_meta' -d '{ 
    "type": "mongodb", 
    "mongodb": { 
        "db": "testmongo", 
        "collection": "person"
    }, 
    "index": {
        "name": "mongoindex", 
        "type": "person" 
    }
}'

add some data to the mongodb through mongo terminal

use testmongo
var p = {firstName: "John", lastName: "Doe"}
db.person.save(p)

Use this command to search the data

curl -XGET 'http://localhost:9200/mongoindex/_search?q=firstName:John'
Posted in Elastic Search, MongoDB | Tagged , , , | Leave a comment

Error whil creating super user in django1.4 on MAC

I got the following error while trying to create a super user from shell for django1.4.

File "/Library/Python/2.7/site-packages/django/contrib/auth/management/__init__.py", line 85, in get_system_username
    return getpass.getuser().decode(locale.getdefaultlocale()[1])
TypeError: decode() argument 1 must be string, not None

This problem was rectified by running this command in terminal before adding the user
export LANG="en_US.UTF-8"

I’m guessing this character encoding problem while running syncdb is the reason for the following error while accessing admin section of django1.4

DoesNotExist at /admin/
Site matching query does not exist.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.4
Exception Type: DoesNotExist
Exception Value:
Site matching query does not exist.
Exception Location: /Library/Python/2.7/site-
packages/Django-1.4-py2.7.egg/django/db/models/query.py in get, line
366
Python Executable: /usr/bin/python
Python Version: 2.7.1
Python Path:
['/Users/mohammedelsebaey/a/mysite1',
'/Library/Python/2.7/site-packages/Django-1.4-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/
python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/
python2.7
/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/
python/PyObjC',
'/Library/Python/2.7/site-packages']

If i’m wrong you can solve the above problem by running this code i django shell

from django.contrib.sites.models import Site
Site.objects.create(pk=1)
Posted in Django, PYTHON | Tagged , | Leave a comment

How to start stop apache on MAC OSX

start Apache
apachectl start
Stop Apache
apachectl stop
Restart Apache
apachectl restart

If the above commands don’t work, try these

/etc/init.d/httpd start
/etc/init.d/httpd stop
/etc/init.d/httpd restart

You can also start the web server from the system preferences. Open system preferences, go to sharing and then enable web sharing

starting webserver in mac osx

starting webserver in mac osx

Posted in Apache | Tagged | Leave a comment

Where is httpd.conf file in Mac OSX Lion

On mac httpd.conf file is located in the directory /etc/apache2/. You can access it through terminal if you want to navigate to it through finder, on finder toolbar goto go->go to folder and type in /etc/apache2

Open hidden folders in mac osx

Navigate to hidden folders in MAC


Navigating to system files on MAC OSX

Navigating to system files on MAC OSX

Posted in OSX, Web Development | Tagged , , | Leave a comment

Make command not found in Mac OSX

If you get a -bash: make: command not found error while trying to compile from source, you have to install xcode and then in xcode goto preferences->downloads and install xcode developer tools

installing command line tools on MAC OSX

installing command line tools on MAC OSX



Download Xcode

I’m not sure if you can install command lines without xcode. I can not test as I already have Xcode on my system you can download command line tools here

Posted in Coding, OSX | Tagged , , , | Leave a comment

When mobile browsers don’t respect CSS3 media queries

If mobile browsers are not respecting your media queries and showing desktop instead of mobile site, add this meta tag before linking the style sheet.

<meta name="viewport" content="width=device-width">

Posted in Web Development | Tagged , , | Leave a comment