From 815c600f6afaa927252f2a5b8de45a279dd898e8 Mon Sep 17 00:00:00 2001 From: Martin Ghazaryan Date: Tue, 6 Jun 2023 12:12:19 +0400 Subject: [PATCH] Fix DeleteOne function bug --- mongo.go | 13 ++++++------- test-insertbatch.js | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/mongo.go b/mongo.go index e3a79c0..2e3b33a 100644 --- a/mongo.go +++ b/mongo.go @@ -3,8 +3,6 @@ package xk6_mongo import ( "context" "log" - "strings" - "errors" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" @@ -39,6 +37,7 @@ func (*Mongo) NewClient(connURI string) interface{} { return &Client{client: client} } +const filter_is string = "filter is "; func (c *Client) Insert(database string, collection string, doc map[string]string) error { db := c.client.Database(database) @@ -66,7 +65,7 @@ func (c *Client) InsertMany(database string, collection string, docs []any) erro func (c *Client) Find(database string, collection string, filter interface{}) []bson.M{ db := c.client.Database(database) col := db.Collection(collection) - log.Print("filter is ", filter) + log.Print(filter_is, filter) cur, err := col.Find(context.TODO(), filter) if err != nil { log.Fatal(err) @@ -83,7 +82,7 @@ func (c *Client) FindOne(database string, collection string, filter map[string]s col := db.Collection(collection) var result bson.M opts := options.FindOne().SetSort(bson.D{{"_id", 1}}) - log.Print("filter is ", filter) + log.Print(filter_is, filter) err := col.FindOne(context.TODO(), filter, opts).Decode(&result) if err != nil { log.Fatal(err) @@ -111,8 +110,8 @@ func (c *Client) DeleteOne(database string, collection string, filter map[string db := c.client.Database(database) col := db.Collection(collection) opts := options.Delete().SetHint(bson.D{{"_id", 1}}) - log.Print("filter is ", filter) - result, err := col.DeleteMany(context.TODO(), filter, opts) + log.Print(filter_is, filter) + result, err := col.DeleteOne(context.TODO(), filter, opts) if err != nil { log.Fatal(err) } @@ -124,7 +123,7 @@ func (c *Client) DeleteMany(database string, collection string, filter map[strin db := c.client.Database(database) col := db.Collection(collection) opts := options.Delete().SetHint(bson.D{{"_id", 1}}) - log.Print("filter is ", filter) + log.Print(filter_is, filter) result, err := col.DeleteMany(context.TODO(), filter, opts) if err != nil { log.Fatal(err) diff --git a/test-insertbatch.js b/test-insertbatch.js index 714db3b..34ed467 100644 --- a/test-insertbatch.js +++ b/test-insertbatch.js @@ -13,7 +13,7 @@ export default () => { docobjs.push(getRecord()); } - client.insertBatch("test", "test", docobjs); + client.insertMany("test", "test", docobjs); } function getRecord() {