Handle both bson.D and bson.M on Find results
This commit is contained in:
20
find.go
20
find.go
@@ -60,8 +60,14 @@ func (c *MongoClient) Find(ctx context.Context, database, name string, filter bs
|
|||||||
|
|
||||||
var totalValue any
|
var totalValue any
|
||||||
if len(metadata) != 0 {
|
if len(metadata) != 0 {
|
||||||
metadataRoot := metadata[0].(bson.D)
|
switch metadata[0].(type) {
|
||||||
totalValue, _ = commons.BsonDGetAny(metadataRoot, "total")
|
case bson.D:
|
||||||
|
metadataRoot := metadata[0].(bson.D)
|
||||||
|
totalValue, _ = commons.BsonDGetAny(metadataRoot, "total")
|
||||||
|
case bson.M:
|
||||||
|
metadataRoot := metadata[0].(bson.M)
|
||||||
|
totalValue = metadataRoot["total"]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
@@ -148,8 +154,14 @@ func (c *MongoClient) FindOffset(ctx context.Context, database, name string, fil
|
|||||||
|
|
||||||
var totalValue any
|
var totalValue any
|
||||||
if len(metadata) != 0 {
|
if len(metadata) != 0 {
|
||||||
metadataRoot := metadata[0].(bson.D)
|
switch metadata[0].(type) {
|
||||||
totalValue, _ = commons.BsonDGetAny(metadataRoot, "total")
|
case bson.D:
|
||||||
|
metadataRoot := metadata[0].(bson.D)
|
||||||
|
totalValue, _ = commons.BsonDGetAny(metadataRoot, "total")
|
||||||
|
case bson.M:
|
||||||
|
metadataRoot := metadata[0].(bson.M)
|
||||||
|
totalValue = metadataRoot["total"]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
|
|||||||
@@ -15,7 +15,9 @@ func TestFind_Default(t *testing.T) {
|
|||||||
client := GetMongoClient()
|
client := GetMongoClient()
|
||||||
|
|
||||||
filter := bson.M{"name": bson.M{"$regex": "OSRAM"}}
|
filter := bson.M{"name": bson.M{"$regex": "OSRAM"}}
|
||||||
findResult, err := client.Find(context.Background(), "mydb", "mycollection", filter, 0)
|
findResult, err := client.Find(context.Background(), "mydb", "mycollection", filter, &FindOptions{
|
||||||
|
Offset: int64(0),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to insertOne %#v", err)
|
t.Fatalf("Failed to insertOne %#v", err)
|
||||||
}
|
}
|
||||||
@@ -74,6 +76,7 @@ func TestFind_Discriminator(t *testing.T) {
|
|||||||
|
|
||||||
client := GetMongoClient()
|
client := GetMongoClient()
|
||||||
client.AddDefinition(store)
|
client.AddDefinition(store)
|
||||||
|
|
||||||
client.AddDefinition(offer)
|
client.AddDefinition(offer)
|
||||||
|
|
||||||
// Save two offers with the similar name in different stores each.
|
// Save two offers with the similar name in different stores each.
|
||||||
@@ -97,7 +100,9 @@ func TestFind_Discriminator(t *testing.T) {
|
|||||||
// Now searching in store str_1234 for OSRAM should return only one
|
// Now searching in store str_1234 for OSRAM should return only one
|
||||||
|
|
||||||
filter := bson.M{"name": bson.M{"$regex": "OSRAM*"}}
|
filter := bson.M{"name": bson.M{"$regex": "OSRAM*"}}
|
||||||
findResult, err := client.Find(ctx1, "mydb", "offer", filter, 0)
|
findResult, err := client.Find(ctx1, "mydb", "offer", filter, &FindOptions{
|
||||||
|
Offset: int64(0),
|
||||||
|
})
|
||||||
if err != nil { t.Fatalf("Failed to find %#v", err) }
|
if err != nil { t.Fatalf("Failed to find %#v", err) }
|
||||||
|
|
||||||
dataAny, hasData := findResult["data"]
|
dataAny, hasData := findResult["data"]
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
// "runtime"
|
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||||
"go.mongodb.org/mongo-driver/v2/bson"
|
"go.mongodb.org/mongo-driver/v2/bson"
|
||||||
@@ -19,8 +17,6 @@ func (c *MongoClient) CreateViews(db *mongo.Database, cdef *CollectionDefinition
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// runtime.Breakpoint()
|
|
||||||
|
|
||||||
for name, defVal := range cdef.Views {
|
for name, defVal := range cdef.Views {
|
||||||
|
|
||||||
// 1. Decode definition
|
// 1. Decode definition
|
||||||
|
|||||||
Reference in New Issue
Block a user