2

I have this callback

p.OnSuccess(func(v interface{}) {

    bulk := collections.Bulk()

    bulk.Insert(v)
    _, bulkErr := bulk.Run()
    if bulkErr != nil {
        panic(bulkErr)
    }

    fmt.Printf("\n - %d comments inserted!", reflect.ValueOf(v).Len())
    Response(w, 200, 1, "comment inserted!", v)
})

Where v is a interface array and when i run the program for insert the data in mongo, golang response me with this message:

BSON field 'insert.documents.0' is the wrong type 'array', expected type 'obje ct'

this is the struct:

type Comment struct {

    CommentId   int64 `bson:"commentId" json:"commentId"`
    From        UserComment `bson:"from" json:"from"`
    Text        string      `bson:"text" json:"text"`
    CreatedTime time.Time   `bson:"createdTime" json:"createdTime"`
    InfId       string      `bson:"infId" json:"infId"`
    PostDate    string      `bson:"postDate" json:"postDate"`
    PostId      string      `bson:"postId" json:"postId"`
    Rate        string      `bson:"rate" json:"rate"`
    CreatedAt   time.Time   `bson:"createdAt" json:"createdAt"`
    UpdatedAt   time.Time   `bson:"updatedAt" json:"updatedAt"`
}

type UserComment struct {

    InstagramId int64  `bson:"instagramId" json:"instagramId"`
    Username    string `bson:"username" json:"username"`
    FullName    string `bson:"fullName" json:"fullName"`
    Picture     string `bson:"picture" json:"picture"`
}

i don't know if is the format of the struct but i tried with this code and it did not work either!

var (
    Allcomments []Comment
    p           = promise.NewPromise()
)

fc := UserComment{
    InstagramId: 1121313, //c.User.ID,
    Username:    "c.User.Username",
    FullName:    "c.User.FullName",
    Picture:     "c.User.ProfilePicURL",
}

cmmnts := Comment{
    CommentId:   44232323, //c.ID,
    From:        fc,
    Text:        "c.Text",
    CreatedTime: now,
    InfId:       "infId",
    PostDate:    "postDate",
    PostId:      "PostId",
    Rate:        "a",
    CreatedAt:   now,
    UpdatedAt:   now,
}



Allcomments = append(Allcomments, cmmnts)
p.Resolve(Allcomments)

1 Answer 1

2

First, I would suggest to use go.mongodb.org/mongo-driver library to interact with MongoDB. This is the MongoDB official driver for the Go language.

To insert an array, you can utilise Collection.InsertMany(). For example:

result, err := coll.InsertMany(
            context.Background(),
            []interface{}{
                bson.D{
                    {"item", "shirt"},
                    {"quantity", int32(25)},
                    {"tags", bson.A{"blank", "red"}},
                    {"size", bson.D{
                        {"h", 14},
                        {"w", 21},
                        {"uom", "cm"},
                    }},
                },
                bson.D{
                    {"item", "hat"},
                    {"quantity", int32(42)},
                    {"tags", bson.A{"gray"}},
                    {"size", bson.D{
                        {"h", 27.9},
                        {"w", 35.5},
                        {"uom", "cm"},
                    }},
                },
}) 

See also Collection.BulkWrite() to perform Bulk Write Operations

Sign up to request clarification or add additional context in comments.

3 Comments

Why do you have context.Backgound?
Function has a context as a first parameter. Using context.Background() (a non-nil, empty Context) is a common practice when you don't need a context, but have to pass it.
how can convert my v interface{} in bson? my struct is how this example ` CommentId int64 json:"commentId" but when i wanna insert the v the problem persist 😓

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.