Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
Put your views on different design documents in couchDB in order to bypass whole DB lock for indexing. Be cautious that if your database has huge amount of data there will be an hourly lock on couchiDB on all operations!!!

So for example if you have couchDB view on:
http://YOUR_IP:5984/_utils/#/database/YOUR_DATABASE/_design/query/_view/THIS_IS_YOUR_VIEW

Put your view that is on a different category like below:
http://YOUR_IP:5984/_utils/#/database/YOUR_DATABASE/_design/NEW_DESIGN_DOCUMENT/_view/THIS_IS_YOUR_VIEW

This simple trick will prevent database lock on whole operations, the change wiill at least does not affect other views operations.

#couchdb #database #performance #lock #indexing
In couchDB you have to plan for views beforehand. As you go along, data will grow and it would be a cumbersome task to change views and wait for data reindexing.

In order to implement a view in couchDB you write JS code. For writing view you have to at least have a map function (reduce can be ignored).

An example of a view to get users by their corresponding uid:

function(doc) {
if (doc.type == "user")
emit(doc.user_id, doc);
}

Here we pass doc which is database documents to our function and check if type of document is user (type field is added it is not related to couchDB view), we will emit (let's say return) user document. It's key is user_id and it's value will be user document.

#view #couchdb
In order to remove a specific document in couchDB, you just need to give delete function the document itself:
couch = couchdb.Server('http://SERVER_IP:PORT/')
your_db = couch['your_db_name']

# we assume you have fetched your_couch_db_doc document
your_db.delete(your_couch_db_doc)

Use this method in preference over __del__ to ensure you're deleting the revision that you had previously retrieved. In the case the document has been updated since it was retrieved, this method will raise a ResourceConflict exception:
>>> db.delete(doc) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ResourceConflict: (u'conflict', u'Document update conflict.')

#couchdb #couch #delete #remove #document #python
Today I had to migrate the ODM class from CouchDB to MongoDB for a specific module. As we didn't combine logic into our backend database we easily changed db to MongoDB.

Go take a look at your code see whether you can change the backend DB easily from your current database to another database? Will you scratch your head when CTO says, ok we are moving to BlahDB, what would you do? How much flexibilty you have?

Some people may argue that we don't change backend DB that much and we do not ever change the backend DB! This a naive consideration and I would highly recommend to think over. Because this code change flexibility shows how well your structure is implemented. How well you have programmed.

#couchdb #mongodb