如何在一个请求中显示这样的 CouchDB Complex 输出?

发布于 2024-11-30 03:18:35 字数 1524 浏览 1 评论 0原文

我只是 couchdb 的初学者,所以我可能会误解观点,所以你可以教我并与我讨论

Doc Type - 用户 - 话题 - 评论

要求 - 我想要网页版 - 1 个获取此复杂文档的请求

输出我需要 KEY "topic-id" , VALUE { _id : "主题 ID",created_at:"2011-05-30 19:50:22", title:"你好 世界”,用户:{_id : “user-1”,类型:“user”,用户名:“dominixz”,签名:“http://dominixz.com”} 评论:[ {_id:"comment-1", text:"评论 1",created_at:"2011-05-30 19:50:22",用户:{_id: "user-1",类型:"user",用户名:"dominixz",签名:"http://dominixz.com"}}, {_id:"comment-2", text:"评论 2",created_at:"2011-05-30 19:50:23",用户:{_id: "user-2",类型:"user",用户名:"dominixz2",签名:"http://dominixz1.com"}}, {_id:"comment-3", text:"评论 3",created_at:"2011-05-30 19:50:24",用户:{_id: "user-3",类型:"user",用户名:"dominixz3",签名:"http://dominixz2.com"}}, ] }

我有这样的“用户”数据 {_id:"user-1",类型:"user",用户名:"dominixz",签名:"http://dominixz.com"} {_id:"user-2",类型:"user",用户名:"dominixz2",签名:"http://dominixz1.com"} {_id:"user-3",类型:"user",用户名:"dominixz3",签名:"http://dominixz2.com"}

像这样的“主题”数据 {_id : "topic-id",created_at:"2011-05-30 19:50:22",标题:"Hello World",用户:"user-1"}

像这样的“评论”数据 {_id:"comment-1",type:"comment" , text:"评论 1",created_at:"2011-05-30 19:50:22", user:"user-1", topic:"topic-id"} {_id:"comment-2",type:"comment" , text:"评论 2", 创建时间:“2011-05-30 19:50:23”,用户:“user-2”,主题:“topic-id”} {_id:"comment-3",type:"评论", text:"评论 3", 创建时间:“2011-05-30 19:50:24”,用户:“user-3”,主题:“topic-id”}

我怎样才能编写map,reduce,list来实现这个复杂的数据?当我想像在 db 中一样使用 LIMIT 、 OFFSET 时怎么样?

提前谢谢

I just the beginner of couchdb so I may be misunderstand point of view so you can teach and discuss with me

Doc Type
- User
- Topic
- Comment

Requirement
- I want to webboard
- 1 Request to get this complex doc

Output I need KEY "topic-id" , VALUE {
_id : "topic-id", created_at:"2011-05-30 19:50:22", title:"Hello
World", user: {_id :
"user-1",type:"user",username:"dominixz",signature:"http://dominixz.com"}
comments: [ {_id:"comment-1", text:"Comment 1",created_at:"2011-05-30
19:50:22",user: {_id :
"user-1",type:"user",username:"dominixz",signature:"http://dominixz.com"}},
{_id:"comment-2", text:"Comment 2",created_at:"2011-05-30
19:50:23",user: {_id :
"user-2",type:"user",username:"dominixz2",signature:"http://dominixz1.com"}},
{_id:"comment-3", text:"Comment 3",created_at:"2011-05-30
19:50:24",user: {_id :
"user-3",type:"user",username:"dominixz3",signature:"http://dominixz2.com"}},
] }

I have "user" data like this
{_id:"user-1",type:"user",username:"dominixz",signature:"http://dominixz.com"}
{_id:"user-2",type:"user",username:"dominixz2",signature:"http://dominixz1.com"}
{_id:"user-3",type:"user",username:"dominixz3",signature:"http://dominixz2.com"}

"Topic" data like this {_id : "topic-id",created_at:"2011-05-30
19:50:22",title:"Hello World",user:"user-1"}

"Comment" data like this {_id:"comment-1",type:"comment" ,
text:"Comment 1", created_at:"2011-05-30 19:50:22" , user: "user-1" ,
topic:"topic-id"} {_id:"comment-2",type:"comment" , text:"Comment 2",
created_at:"2011-05-30 19:50:23" , user: "user-2" , topic:"topic-id"}
{_id:"comment-3",type:"comment" , text:"Comment 3",
created_at:"2011-05-30 19:50:24" , user: "user-3" , topic:"topic-id"}

How can I write map,reduce,list for achieve this complex data ? and how about when I wanna use LIMIT , OFFSET like in db

Thank in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-12-07 03:18:35

很难说出您在这里寻找什么,但我认为您正在要求一个经典的 CouchDB 连接,如 此网页

我建议阅读整篇文章,但要点看起来像这样(针对您的数据进行翻译):

function (doc) {
    if (doc.type === 'topic') {
        emit([doc._id, 0, doc.created_at], null);
    } else if (doc.type === 'comment') {
        emit([doc._id, 1, doc.created_at], null);
    }
}

该地图将返回主题 ID,后跟按时间顺序排列的所有评论。 null 可以防止索引变得太大,您可以随时在请求中添加 include_docs=true 以在需要时提取完整文档, 您可以使用索引最佳实践,包括其中感兴趣的位。

It's a bit hard to tell what you're looking for here, but I think you're asking for a classic CouchDB join as documented in this web page.

I'd recommend reading the whole thing, but the punchline looks something like this (translated for your data):

function (doc) {
    if (doc.type === 'topic') {
        emit([doc._id, 0, doc.created_at], null);
    } else if (doc.type === 'comment') {
        emit([doc._id, 1, doc.created_at], null);
    }
}

That map will return the topic ID followed by all of its comments in chronological order. The null prevents the index from getting too large, you can always add include_docs=true on your request to pull full docs when you need them, or you can use index best practices of including the bits that are interesting there.

ぶ宁プ宁ぶ 2024-12-07 03:18:35

CouchDB 是一个文档数据库,而不是关系数据库。因此,它最适合处理包含所有相关数据的文档。虽然您可以像您一样标准化您的模式关系样式,但我认为这不是 Couch 的最佳用例。

如果我要在 Couch 中设计您的 CMS,我会将主题、内容和评论全部保留在一个文档中。那可以直接解决你的问题。

当然,您可以自由地使用文档存储来模拟关系数据库,但这不是它们的自然用例,这会导致像这样的问题。

CouchDB is a document database, not a relational database. As such it is best suited to deal with documents that encompass all the related data. While you can normalize your schema relational-style like you did, I'd argue that this isn't be best use case for Couch.

If I were to design your CMS in Couch I'd keep the topic, content and comments all in a single document. That would directly solve your problem.

You're free of course to use document stores to emulate relational databases, but that's not their natural use case, which leads to questions like this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文