Set modified via bson marshal and update operators
This commit is contained in:
@@ -1,7 +1,16 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
const (
|
||||
Name = "_id"
|
||||
JSONName = "name"
|
||||
Relationship = "relationship"
|
||||
Type = "type"
|
||||
Title = "title"
|
||||
@@ -33,3 +42,22 @@ func (o One) Peers() []string {
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func (o One) MarshalBSON() ([]byte, error) {
|
||||
if fmt.Sprint(o) != fmt.Sprint(o.Query()) {
|
||||
o.Modified = time.Now().UnixNano()
|
||||
}
|
||||
b, err := json.Marshal(o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := bson.M{}
|
||||
if err := json.Unmarshal(b, &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if name, ok := m[JSONName]; ok {
|
||||
m[Name] = name
|
||||
delete(m, JSONName)
|
||||
}
|
||||
return bson.Marshal(m)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user