Spring Mongo Repository 无法在 _id 字段中将 String 与 ObjectId 匹配
我有一个带有 String Id 的数据类,我将它保存到带有空值的 mongoDB 中以获取自动生成的替换.
I have a data class with a String Id and I'm saving it into the mongoDB with a null value to get an autogenerated replacement.
@Document(collection="applications")
@TypeAlias(Constants.Application)
public class DataApplication implements IApplication{
@Id
@Field("id")
private String id = null;
...
}
这按预期工作,但是我面临一个问题,我似乎无法从 Spring Mongo Repo 中找到 findById.它能够毫无问题地持久化和检索,创建 BSON 之类的
This works as expected however I'm facing a problem in that I cannot seem to findById from the Spring Mongo Repo. It is capable of persisting and retrieving without problems, creating BSON like
{
"_id" : ObjectId("5ef9a579bafa09b36225e743c"),
Other fields and things
}
然而,这似乎是关于尝试使用 id 字段进行查找的.很奇怪,它没有使用我指定的 Id 字段名称,但是当我一次检索多个对象时,它们按预期转换回 POJO,我认为这不是问题.
However, this seems to be specifically about when attempting to find using the id field. Its very weird that it isn't using the Id field name I specified, however as when I retrieve multiple objects at once they convert back into POJOs as expected I do not believe this is an issue.
从我所见,这似乎是 MongoDB 将值存储为 ObjectId 的结果,因为当我保留具有 String Id 值的对象时,我能够通过 Id 检索,当我将应用程序拉入 Java 以比较,或者当我创建匹配除 Id 之外的所有字段的示例时,但是当我设置一个 Id 进行比较时,示例失败.
From what I can see this seems to be a result of the MongoDB storing the values as ObjectId's, as I am able to retrieve by Id when I persist an object with a String Id value, when I pull the Applications into Java to compare, or when I create Examples that match all fields except Id, however Examples fails when I set an Id to compare against.
防止 Mongo 的 Spring Data 将 id 转换为ObjectId 似乎是相关的,因为它提到了底层查询结构,不幸的是我似乎有相反的问题.
Prevent Spring Data for Mongo to convert ids to ObjectId seems to be related as it mentions the underlying query structure, unfortunately I seem to have the opposite problem.
@Field
注释用于定义文档字段的自定义元数据.例如:
@Field
annotation is used to define custom metadata for document fields. For example:
@Field(targetType = DECIMAL128)
private BigDecimal value;
@Field("fName")
private String firstName;
当您定义 @Field("id") private String id = null;
时,它被解释为字段 id
作为另一个字段 id
不是文档标识符字段 _id
.因为,注释的 name
属性被指定为id".您也可以在以下示例中使用 Person
Pojo 类验证这一点:
When you defined @Field("id") private String id = null;
it is interpreted as the field id
as another field id
not the document identifier field _id
. Because, the annotation's name
attribute is specified as "id". You can verify this as well in the following example using the Person
Pojo class:
public class Person {
@Field("id")
private String id = null;
private String name;
public Person(String name) {
this.name = name;
}
public void setId(String id) {
this.id = id;
}
// other get / set methods ...
public String toString() {
return "id: " + id + ", " + name;
}
}
使用MongoRepository
方法在person
集合中插入和查询文档:
Using MongoRepository
methods insert and query documents in person
collection:
Person p1 = new Person("J.Doe");
//p1.setId("000");
repo.insert(p1);
List<Person> list = repo.findAll();
list.forEach(System.out::println);
这将插入一个文档(从 mongo
shell 查询)并打印 toString
表示为:
This will insert a document (as queried from the mongo
shell) and print the toString
representation as:
{ "_id" : ObjectId("5ef5c92b70babc5a961350e5"), "name" : "J.Doe", "_class" : "com.example.demo.Person" }
打印为:id: null, J.Doe
如果您取消注释代码 p1.setId("000");
并再次运行它,您会发现以下内容:
If you un-comment the code p1.setId("000");
and run it again, you will find the following:
{ "_id" : ObjectId("5ef5cab8306d071b13d3c16e"), "id" : "000", "name" : "J.Doe", "_class" : "com.example.demo.Person" }
并且,打印为:id: 000, J.Doe
代码运行正常.注意两个字段,_id
和 id
.@Field
用于自定义文档字段.
The code is behaving alright. Note the two fields, the _id
and id
. The @Field
is to be used for customizing a document field.
要正确指示 id
字段将用于 _id
文档键字段,您可以使用以下内容.创建文档时,_id
值将是驱动程序生成的 ObjectId
:
To properly indicate the id
field is to be used for _id
document key field you can use the following. When the document is created the _id
value will be the driver generated ObjectId
:
- 将
id
字段注释为@Id
、@MongoId
或@Field
(不带name
属性). - 完全没有注释.
- Annotate the
id
field as@Id
,@MongoId
or the@Field
(without thename
attribute). - Not annotate at all.
注释说明:
- @标识:在字段级别应用以标记用于标识的字段目的.
- @MongoId:在字段级别应用以标记用于标识的字段目的.接受可选的 FieldType 以自定义 ID 转换.
- @Field:应用于字段级别,它允许描述名称和字段的类型,因为它将在 MongoDB BSON 中表示文档因此允许名称和类型不同于类的字段名称以及属性类型.
- @Id: Applied at the field level to mark the field used for identity purpose.
- @MongoId: Applied at the field level to mark the field used for identity purpose. Accepts an optional FieldType to customize id conversion.
- @Field: Applied at the field level it allows to describe the name and type of the field as it will be represented in the MongoDB BSON document thus allowing the name and type to be different than the field name of the class as well as the property type.