[英]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. 我在CouchDB中有两个数据库-DB1的文档是用户数据-名称,电子邮件地址,用户名,密码,但是在一个字段中存储了ID2的列表,该ID保存在保存用户项目的DB2中(包含用户名字段和一些文本字段)。
Example DB1 Document (Users)
{
"_id": "bobsmith1000",
"_rev": "83-1e00173cac0e736c9988d3addac403de",
"first_name": "Bob",
"password": "$2a$12$sdZUkkyDnDePQFNarTTgyuUZS6DL13JvBk/k9iUa5jh08gWAS5hpm",
"second_name": "Smith",
"urls": null,
"email": "bob@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. 我正在尝试使用Python(使用Flask Web框架和Python Couchdb库)编写一个视图,该视图将检查db1,获取所有项目ID,然后转到db2,并以批处理的方式提取URL,时间和文件大小每个具有匹配ID的文档,这样我就可以将该数据放在表格中。
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"].
在视图功能中,您只需要发出诸如[“ username”,0]和[“ username”,“ projectname”]的键即可。 In Javascript that would be:
用Javascript将是:
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. 您可以查询该视图并拥有一组有组织的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.