mongoDB ObjectID
3369 ワード
ObjectIDとは?
_id
フィールドがあります.idは任意のタイプであってもよいが、集合内で一意でなければならない._id
のデフォルトタイプはObjectIDタイプであり、開発者がObjectIDを入力していない場合はMongoDBドライバによって作成されます.ObjectIDを含むオブジェクトの作成
type OjbectIDTestStruct struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Name string
}
作成時にObjectIDは更新されません。
testStruct := OjbectIDTestStruct{Name: "test1"}
// testSturct.ID = []uint{0,0,0,0,0,0,0,0}
collection.InsertOne(ctx, &testStruct)
// testSturct.ID = []uint{0,0,0,0,0,0,0,0}
結果からObjectIDを選択して更新できます。
testStruct := OjbectIDTestStruct{Name: "test1"}
// testSturct.ID = []uint{0,0,0,0,0,0,0,0}
result, err := collection.InsertOne(ctx, &testStruct)
testStruct.ID = result.InsertedID
// testStruct.ID = []uint{97,206,146,190,8,...}
created atをポップアップ
myStruct.ID.Timestamp
(概ね)create atの順序と似ている。
While ObjectId values should increase over time, they are not necessarily monotonic. This is because they:
Only contain one second of temporal resolution, so ObjectId values created within the same second do not have a guaranteed ordering, and Are generated by clients, which may have differing system clocks.
Reference
この問題について(mongoDB ObjectID), 我々は、より多くの情報をここで見つけました https://velog.io/@moonyoung/mongoDB-ObjectID-with-golangテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol