开发者

Pull data from one couchdb doc via ids in another (Python)

I have two databases in CouchDB - DB1's documents are user data - name, email address, username, password but within one field that I store a list of the ID's saved in DB2 where user projects are saved (containing a username field and some Textfields.

    Example DB1 Document (Users)

       {
   "_id": "bobsmith1000",
   "_rev": "83-1e00173cac0e736c9988d3addac403de",
   "first_name": "Bob",
   "password": "$2a$12$sdZUkkyDnDePQFNarTTgyuUZS6DL13JvBk/k9iUa5jh08gWAS5hpm",
   "second_name": "Smith",
   "urls": null,
   "email": "b开发者_如何学Goob@smith.com",
   "projects": [
       "ee5ccf56da22121fd71d892dbe051746",
       "ee5ccf56da22121fd71d892dbe0526bb",
       "ee5ccf56da22121fd71d892dbe053433",
       "ee5ccf56da22121fd71d892dbe056c71",
       "ee5ccf56da22121fd71d892dbe0579c3",
       "ee5ccf56da22121fd71d892dbe05930d"
   ]
}

Example DB2 Document (Projects)

{
   "_id": "ee5ccf56da22121fd71d892dbe05930d",
   "_rev": "1-c923fbe9de82318980c7778c4c089321",
   "url": "http://harkmastering.s3.amazonaws.com/testprojects/testfolder.zip",
   "username": "bobsmith1000",
   "time": "2010-10-29 07:13:47.377085",
   "file_size": "5.2 MB"
}

I am trying to write a view in Python (using the Flask web framework and Python Couchdb library) that will check db1, grab all the project ids, and then go to db2 and in a batch way pull out the url, time, filesize for each document with matching ids so I can can place that data in a table.

I only began programming earlier this year and this involves techniques that I can only imagine. Can anyone help me find a solution?

Kind thanks


Unless you have great reasons to split your docs in different dbs, that seems like is not the case according to your question, them you should keep them all on the same database and have some property on the docs to identify their type (like a "type: user" and "type: project").

That way you can get the desired result on a single couchdb view without too much trouble. In your view map function you will just need to emit keys like ["username",0] and ["username","projectname"]. In Javascript that would be:

function(doc) {
 if (doc.type == "user") {
  emit([doc._id,0],null);
 } else if(doc.type == "project") {
  emit([doc.username,doc._id],null);
 }
}

Them you can query that view and have an organized set of documents.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜