映射减少沙发数据库

发布于 2024-12-09 08:12:31 字数 249 浏览 0 评论 0原文

我有一个带有 couch db 的简单数据库。我的用户有该字段:

String userName
String password
String mail
boolean admin

我将我的用户保留在数据库中。然而我对 Couch DB 和 NoSql 很陌生。我如何通过示例在其上实现地图缩减(或者它在内部执行并且我不需要执行任何操作?)

我在我的应用程序中使用 Spring 3 和 Ekorp。

I have a simple database with couch db. I have users have that fields:

String userName
String password
String mail
boolean admin

I keep my users at db. However I am so new to Couch DB and NoSql. How can I implement map reduce on it with an example (or does it do it internally and I don't need to do anything?)

I use Spring 3 and Ekorp for my application.

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

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

发布评论

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

评论(2

以酷 2024-12-16 08:12:31

您的问题可以使用更多细节,但据我了解,您想要创建一个像用户“表”一样的 Couch DB 文档。为此,您需要在 Couch DB 上创建一个具有以下属性的 JSON 文档:

{
    type: "user",
    username: "John",
    password: "*****",
    mail: "blah, blah, blah",
    admin: true
}

正如 lukecampbell 上面指出的那样,您需要创建自己的“类型”属性来标识每个实体...例如,您可以只更改“type”属性,而不是使用管理属性来识别具有权限的用户:

{
    type: "admin"...
}

要查询数据库并检索特定文档,您需要使用映射函数。
卢克坎贝尔的示例再次提供了一个很好的起点:

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

这个“查询”将为您提供每个“用户”实体文档的“视图”,按“doc.name”排序。这就是 Couch DB 的魅力变得显而易见的地方...Map(和reduce)函数在某种意义上就像 SQL 查询...但是因为它们是用 JavaScript 编写的,所以您可以使用 JavaScript 逻辑来塑造您的数据库:

function(doc){
     if(doc.type == "user" && doc.name == "John"){ 
         emit(doc.name, doc);
     }
}

这将给出您可以查看每个用户名为 John 的“用户”文档。
正如您所看到的,在某些情况下,以这种方式编写查询比 SQL 更具表现力...甚至可以通过这种方式“隐藏”类型文档字段;

function(doc){
    if(
        doc.type == "user" && 
        typeof doc.admin == "boolean"
    ){ 
        emit(doc.name, "Is Admin?: " + JSON.stringify(doc.admin);
    }
}

这个“查询”应该打印出每个“用户”的名称以及每个用户文档的一些状态消息(“Is Admin?:true(或 false)”),其中包含 admin 属性的布尔值。至于“Reduce”函数,它们非常有用,但更重要的是首先完全理解映射函数,因为它们非常通用,并且在大多数情况下,编写良好的映射函数将消除编写“reduce”函数的需要...

Couch DB 具有巨大的潜力。这个网站可能是最好的起点; http://guide.couchdb.org/index.html - 也就是说,还有该技术中的一些非常奇怪的怪癖有时会迫使您重新考虑数据库设计...这里简要讨论其中一个怪癖(请参阅 Victor Nicollet 的回答):

使用以下命令创建 Couch DB 视图有什么问题吗空值?

Your question could use a little more detail, but as I understand it, you want to create a Couch DB document that acts like "table" of users. To do that, you will need to create a JSON document on your Couch DB with the following properties:

{
    type: "user",
    username: "John",
    password: "*****",
    mail: "blah, blah, blah",
    admin: true
}

Just as lukecampbell had pointed out above, you will need to create your own "type" property to identify each entity... For example, rather than using an admin property to identify users with privileges , you could just change the "type" property:

{
    type: "admin"...
}

To query the database, and retrieve specific documents, you'll need to use a map function.
Again lukecampbell's example provides a good starting point:

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

This 'query' will give you a "view" of every "user" entity document, ordered by "doc.name". Here's where the beauty of Couch DB becomes apparent... Map (and reduce) functions are like SQL queries in a certain sense... But because they are written in JavaScript, you can use JavaScript logic to shape your database:

function(doc){
     if(doc.type == "user" && doc.name == "John"){ 
         emit(doc.name, doc);
     }
}

This will give you a view of every "user" document where the user's name is John.
As you can see, in some cases, writing queries in this way can be far more expressive than SQL... It's even possible to "duck" type document fields in this way;

function(doc){
    if(
        doc.type == "user" && 
        typeof doc.admin == "boolean"
    ){ 
        emit(doc.name, "Is Admin?: " + JSON.stringify(doc.admin);
    }
}

This "query" should print out the name of each "user" and a little status message ("Is Admin?: true (or false)") for every user document with a boolean value for the admin property. As for "Reduce" functions, they are extremely useful, but it's much more important to understand map functions fully first, because they are VERY versatile, and in most cases a well written map function will obviate the need to write a "reduce" function...

There's LOADS of potential in Couch DB. This site is probably the best place to start; http://guide.couchdb.org/index.html - That said, there are also some very strange quirks in the technology that will occasionally force you to rethink your database designs... One of those quirks are discussed briefly here (see Victor Nicollet's answer):

Is there anything wrong with creating Couch DB views with null values?

七禾 2024-12-16 08:12:31

在沙发上,
在您的文档中添加一个名为

_类型

类型_

函数可以是

function(doc) {
 if(doc.type_=="user") { 
   emit(doc.name,doc._id);
 }
}

in couch,
add a field in your documents called

_type

or

type_

The map function could be

function(doc) {
 if(doc.type_=="user") { 
   emit(doc.name,doc._id);
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文