@@ -4,12 +4,12 @@ package entity
44
55import (
66 "context"
7+ "errors"
78 "time"
89
9- "go.mongodb.org/mongo-driver/bson"
10- "go.mongodb.org/mongo-driver/bson/primitive"
11- "go.mongodb.org/mongo-driver/mongo"
12- "go.mongodb.org/mongo-driver/mongo/options"
10+ "go.mongodb.org/mongo-driver/v2/bson"
11+ "go.mongodb.org/mongo-driver/v2/mongo"
12+ "go.mongodb.org/mongo-driver/v2/mongo/options"
1313
1414 "github.com/square/etre"
1515 "github.com/square/etre/cdc"
@@ -58,7 +58,17 @@ func (s store) ReadEntities(ctx context.Context, entityType string, q query.Quer
5858 // "es -u node.metacluster zone=pd" returns a list of unique metacluster names.
5959 // This is 10x faster than "es node.metacluster zone=pd | sort -u".
6060 if len (f .ReturnLabels ) == 1 && f .Distinct {
61- values , err := c .Distinct (ctx , f .ReturnLabels [0 ], Filter (q ))
61+ dr := c .Distinct (ctx , f .ReturnLabels [0 ], Filter (q ))
62+ if err := dr .Err (); err != nil {
63+ nfe := mongo .ErrNoDocuments
64+ if errors .Is (err , nfe ) {
65+ // No documents found, return empty slice
66+ return []etre.Entity {}, nil
67+ }
68+ return nil , s .dbError (ctx , err , "db-read-distinct" )
69+ }
70+ var values []string
71+ err := dr .Decode (& values )
6272 if err != nil {
6373 return nil , s .dbError (ctx , err , "db-read-distinct" )
6474 }
@@ -122,7 +132,7 @@ func (s store) CreateEntities(ctx context.Context, wo WriteOp, entities []etre.E
122132
123133 now := time .Now ().UnixNano ()
124134 for i := range entities {
125- entities [i ]["_id" ] = primitive .NewObjectID ()
135+ entities [i ]["_id" ] = bson .NewObjectID ()
126136 entities [i ]["_type" ] = wo .EntityType
127137 entities [i ]["_rev" ] = int64 (0 )
128138 entities [i ]["_created" ] = now
@@ -132,7 +142,7 @@ func (s store) CreateEntities(ctx context.Context, wo WriteOp, entities []etre.E
132142 if err != nil {
133143 return newIds , s .dbError (ctx , err , "db-insert" )
134144 }
135- id := res .InsertedID .(primitive .ObjectID )
145+ id := res .InsertedID .(bson .ObjectID )
136146 newIds = append (newIds , id .Hex ())
137147
138148 // Create a CDC event.
@@ -195,7 +205,7 @@ func (s store) UpdateEntities(ctx context.Context, wo WriteOp, q query.Query, pa
195205 }
196206 opts := options .FindOneAndUpdate ().SetProjection (p )
197207
198- nextId := map [string ]primitive .ObjectID {}
208+ nextId := map [string ]bson .ObjectID {}
199209 for cursor .Next (ctx ) {
200210 if err := cursor .Decode (& nextId ); err != nil {
201211 return diffs , s .dbError (ctx , err , "db-cursor-decode" )
@@ -222,7 +232,7 @@ func (s store) UpdateEntities(ctx context.Context, wo WriteOp, q query.Query, pa
222232
223233 cp := cdcPartial {
224234 op : "u" ,
225- id : orig ["_id" ].(primitive .ObjectID ),
235+ id : orig ["_id" ].(bson .ObjectID ),
226236 rev : orig .Rev () + 1 ,
227237 old : & old ,
228238 new : & patch ,
@@ -266,7 +276,7 @@ func (s store) DeleteEntities(ctx context.Context, wo WriteOp, q query.Query) ([
266276 deleted = append (deleted , old )
267277 ce := cdcPartial {
268278 op : "d" ,
269- id : old ["_id" ].(primitive .ObjectID ),
279+ id : old ["_id" ].(bson .ObjectID ),
270280 old : & old ,
271281 new : nil ,
272282 rev : old .Rev () + 1 ,
@@ -286,7 +296,7 @@ func (s store) DeleteLabel(ctx context.Context, wo WriteOp, label string) (etre.
286296 panic ("invalid entity type passed to DeleteLabel: " + wo .EntityType )
287297 }
288298
289- id , _ := primitive .ObjectIDFromHex (wo .EntityId )
299+ id , _ := bson .ObjectIDFromHex (wo .EntityId )
290300 filter := bson.M {"_id" : id }
291301 update := bson.M {
292302 "$unset" : bson.M {label : "" }, // removes label, Mongo expects "" (see $unset docs)
@@ -322,7 +332,7 @@ func (s store) DeleteLabel(ctx context.Context, wo WriteOp, label string) (etre.
322332
323333 cp := cdcPartial {
324334 op : "u" ,
325- id : old ["_id" ].(primitive .ObjectID ),
335+ id : old ["_id" ].(bson .ObjectID ),
326336 new : & new ,
327337 old : & old ,
328338 rev : old .Rev () + 1 ,
@@ -355,7 +365,7 @@ func (s store) dbError(ctx context.Context, err error, errType string) error {
355365// which makes a complete CDCEvent from the partial and a WriteOp.
356366type cdcPartial struct {
357367 op string
358- id primitive .ObjectID
368+ id bson .ObjectID
359369 old * etre.Entity
360370 new * etre.Entity
361371 rev int64
0 commit comments