add object id funcs

This commit is contained in:
Nam Huynh 2021-08-09 11:20:25 +07:00
parent c046d4d2fe
commit 449f377c95
1 changed files with 25 additions and 0 deletions

25
object_id.go Normal file
View File

@ -0,0 +1,25 @@
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()
}
// ConvertFromString ...
func ConvertFromString(s string) (value primitive.ObjectID, isValid bool) {
id, err := primitive.ObjectIDFromHex(s)
return id, err == nil
}
// IsValid ...
func IsValid(s string) bool {
return primitive.IsValidObjectID(s)
}