mongodb/object_id.go

26 lines
540 B
Go
Raw Permalink Normal View History

2021-08-09 04:20:25 +00:00
package mongodb
import "go.mongodb.org/mongo-driver/bson/primitive"
// NewObjectID generates a new ObjectID based on the timestamp.
func NewObjectID() primitive.ObjectID {
return primitive.NewObjectID()
}
// NewStringID ...
func NewStringID() string {
return NewObjectID().Hex()
}
2021-08-09 04:56:18 +00:00
// NewIDFromString ...
func NewIDFromString(s string) (value primitive.ObjectID, isValid bool) {
2021-08-09 04:20:25 +00:00
id, err := primitive.ObjectIDFromHex(s)
return id, err == nil
}
2021-08-09 04:56:18 +00:00
// IsValidID ...
func IsValidID(s string) bool {
2021-08-09 04:20:25 +00:00
return primitive.IsValidObjectID(s)
}