Hernan Humaña
Hernan Humaña

Reputation: 433

How to insert multi array in mongodb with golang

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)

Upvotes: 2

Views: 4211

Answers (1)

Wan B.
Wan B.

Reputation: 18845

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

Upvotes: 2

Related Questions