Couchbase文档ID生成 [英] Couchbase document id generation

查看:104
本文介绍了Couchbase文档ID生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集成了ouchbase 6.0的Springboot应用程序. 我已经读过,如果一个键用@Id注释,那么它将被另存为文档ID,并且不会成为json的一部分.

I have an Springboot application integrated with couchbase 6.0. I have read that if a key is annotated with @Id then it will saved as an document id and will not be a part of the json.

然后我在键上一起使用了@Id和@Field,但该字段仍未出现在json文档中.

Then i have used @Id and @Field together on the key but still that field is not appearing on json document.

有人可以帮我解决以下问题吗?

Can somebody help me out on the following:

1:如何使用与文档ID相同的密钥,并将其保留为json的一部分.

1: How to make same key as document id and keep that a part of json also.

2:如果使用@Id声明字段,则将其创建为文档ID并执行 没有出现在文档中,但是当我执行获取请求时,相同的键出现在文档中 响应.

2: If a field is declared with @Id, it is created as document id and does not appear on document, but when i do a get request same key appear on the response.

我也尝试过,请单击此处

I have also tried, click here https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration.usingattributes

我的实体是:

@Id @GeneratedValue(strategy = GenerationStrategy.USE_ATTRIBUTES,delimiter="::")
private String id;
@IdAttribute
private String userid;
@Field
private String fname;
@Field
private String lname;

发布方法正文:

{

"id": "888",
"userid":"user1",
"fname": "xyz",
"lname": "abc"

}

在Couchbase服务器中,该文档另存为

In Couchbase server, it is saving this document as

仅创建文档ID为 888 ,它应生成文档ID为 888 :: user1

It is creating document id as 888 only, it is supposed to generate documnet id as 888::user1

我已经测试了很多次,但是结果是一样的.

I have tested this many times but the result is same.

推荐答案

看起来像您在寻找

通常的做法是结合使用 文档属性.使用属性的密钥生成将所有内容串联在一起 根据顺序用IdAttribute注释的属性值 提供的类似于前缀和后缀.

It is a common practice to generate keys using a combination of the document attributes. Key generation using attributes concatenates all the attribute values annotated with IdAttribute, based on the ordering provided similar to prefixes and suffixes.

例如,您可以这样做:

@Document
public class User {
  @Id @GeneratedValue(strategy = USE_ATTRIBUTES)
  private String id;
  @IdAttribute
  private String userid;
  ...
}

根据上面的示例,您将得到一个带有id = userId以及json中的字段userId的文档.

According to the example above you will get as a result a Document with the id = userId and also the field userId in the json.

按问题编辑更新

您可能必须在您的实体中包括一个新文件,例如postId,然后用postId更改post方法主体中id字段的命名,例如:

You may have to include a new filed in your entity, let's say postId and change the naming of the id field in your post method body by postId, something like:

实体:

@Id @GeneratedValue(strategy = GenerationStrategy.USE_ATTRIBUTES,delimiter="::")
private String id;
@IdAttribute(order = 0)
private String postId;
@IdAttribute(order = 1)
private String userid;
@Field
private String fname;
@Field
private String lname;

发布方法正文:

{
"postId": "888",
"userid":"user1",
"fname": "xyz",
"lname": "abc"
}

这篇关于Couchbase文档ID生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆