gpt4 book ai didi

MongoDB Grails 插件 ObjectId V.S.字符串编号

转载 作者:行者123 更新时间:2023-12-02 14:45:02 25 4
gpt4 key购买 nike

我有一个关于 grails 的 MongoDB id 的问题。我在域类中使用了字符串 id,它会在域类中创建字符串 id,这很好。但是当我手动在 mongodb 中插入条目时(运行 db.Things.insert()),它会为新条目创建一个 ObjectId(不是字符串),我想知道我们如何解决这个问题。

谢谢!

最佳答案

引用来自:Mongo Collections - Reference Documentation

By default in GORM entities are supplied with an integer-based identifier. So for example the following entity: class Person {} Has a property called id of type java.lang.Long. In this case GORM for Mongo will generate a sequence based identifier using the technique described in the Mongo documentation on Atomic operations.

However, sequence based integer identifiers are not ideal for environments that require sharding (one of the nicer features of Mongo). Hence it is generally advised to use either String based ids:

class Person { String id } Or a native BSON ObjectId:

import org.bson.types.ObjectId class Person { ObjectId id }

BSON ObjectId instances are generated in a similar fashion to UUIDs.


因此,您应该使用 org.bson.types.ObjectId 替换您的域类的 id

关于MongoDB Grails 插件 ObjectId V.S.字符串编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15479105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com