2015-03-02 47 views
1

我是CouchDb的新手。當我安裝它時,我認爲它會像MongoDb一樣,但是mongo現在似乎比沙發更透明。至少,在mongo中,我可以使用find()在沙發中幾乎馬上插入和獲取數據,但是我沒有看到這樣簡單的查詢方法。所以,想象一下,我保存了一份文檔SELECT ... WHERE ...查詢在CouchDB + Python

{'type':'post','theme':'blabla'} 

現在我需要查詢所有帖子。我該怎麼做Python(使用couchdb模塊)?

+1

查詢完成後通過在CouchDB中,一個完全不同的概念,「意見」的工作。最後你不遍歷數據,你遍歷由View創建的B-Tree。從SQL到CouchDB的解釋可以在這裏找到:http://guide.couchdb.org/draft/cookbook.html – h4cc 2015-03-02 21:38:53

回答

2

首先,嘗試創建一個視圖。

function(doc) { 
    if(doc.type) { 
    emit(doc.type); 
    } 
} 

這裏看到更多的信息訪問量:http://guide.couchdb.org/draft/views.html

接下來,編寫一些Python。我只有用cloudant-python庫經驗,這將是這個樣子:

import cloudant 

account = cloudant.Account('http://localhost:5984') 
db = account.database('yourdb') 

view = db.view('theview') 
options = { 
    'key': 'post', 
    'include_docs': True 
} 
for row in view.iter(params=options): 
    # emits only rows with the key 'post' 
    # with each row's emitting document 

注:這也應該對CouchDB的