2

I try to read and write and delete data from a Go application with the official mongodb driver for go (go.mongodb.org/mongo-driver).

Here is my struct I want to use:

Contact struct {
    ID               xid.ID `json:"contact_id" bson:"contact_id"`
    SurName          string `json:"surname" bson:"surname"`
    PreName          string `json:"prename" bson:"prename"`
}
// xid is https://github.com/rs/xid

I omit code to add to the collection as this is working find.

I can get a list of contacts with a specific contact_id using the following code (abbreviated):

filter := bson.D{}
cursor, err := contactCollection.Find(nil, filter)
for cur.Next(context.TODO()) {
  ...
}

This works and returns the documents. I thought about doing the same for delete or a matched get:

// delete - abbreviated
filter := bson.M{"contact_id": id}
result, _ := contactCollection.DeleteMany(nil, filter)
// result.DeletedCount is always 0, err is nil
if err != nil {
    sendError(c, err) // helper function
    return
}

c.JSON(200, gin.H{
    "ok":      true,
    "message": fmt.Sprintf("deleted %d patients", result.DeletedCount),
}) // will be called, it is part of a webservice done with gin

// get complete
func Get(c *gin.Context) {
    defer c.Done()

    id := c.Param("id")
    filter := bson.M{"contact_id": id}

    cur, err := contactCollection.Find(nil, filter)
    if err != nil {
        sendError(c, err) // helper function
        return
    } // no error

    contacts := make([]types.Contact, 0)
    for cur.Next(context.TODO()) { // nothing returned
        // create a value into which the single document can be decoded
        var elem types.Contact
        err := cur.Decode(&elem)
        if err != nil {
            sendError(c, err) // helper function
            return
        }
        contacts = append(contacts, elem)
    }

    c.JSON(200, contacts)
}

Why does the same filter does not work on delete?

Edit: Insert code looks like this:

_, _ = contactCollection.InsertOne(context.TODO(), Contact{
    ID: "abcdefg",
    SurName: "Demo",
    PreName: "on stackoverflow",
})
Sascha
  • 10,231
  • 4
  • 41
  • 65
  • What is `xid.ID`? Can you post the type declaration and its methods? Also, `DeleteMany()` returns an error, do not omit it, what is the error? It could be you have no permission to remove documents... – icza Jun 19 '19 at 07:09
  • I omitted the error as it returns no error – Sascha Jun 19 '19 at 07:09
  • Also you have to use `bson` tag, not `json`. Please either change `json` to `bson` in your `Contact` struct, or if you use JSON as well, add `bson` too. – icza Jun 19 '19 at 07:11
  • @icza Updated with more code – Sascha Jun 19 '19 at 07:20
  • It looks you use string `id` with `Find()`, is it the same with `DeleteMany()`? Also, have you tried with `bson` tags? – icza Jun 19 '19 at 07:21
  • @icza added the bson tags (bson:"surname") in addition to the json one's. As inserting worked with xid I did change that for querying, it gets converted to string, so yes the same – Sascha Jun 19 '19 at 07:24
  • There seems to be some inconsistency. `Contact.ID` is of type `xid.ID` (which is a byte array), but when you insert a `Contact`, you simply use a `string` literal. That would be a compile-time error. – icza Jun 19 '19 at 07:27
  • @icza no compile error, as the insert does unmarshal from post payload. Anyhow you were on the right track. As soon as I switched the type to string everywhere and used Stringer for ID and changed the type it worked. Please post it as an answer for future reference so I can accept it – Sascha Jun 19 '19 at 07:30

2 Answers2

2

Contact.ID is of type xid.ID, which is a byte array:

type ID [rawLen]byte

So the insert code you provided where you use a string literal to specify the value for the ID field would be a compile-time error:

_, _ = contactCollection.InsertOne(context.TODO(), Contact{
    ID: "abcdefg",
    SurName: "Demo",
    PreName: "on stackoverflow",
})

Later in your comments you clarified that the above insert code was just an example, and not how you actually do it. In your real code you unmarshal the contact (or its ID field) from a request.

xid.ID has its own unmarshaling logic, which might interpret the input data differently, and might result in an ID representing a different string value than your input. ID.UnmarshalJSON() defines how the string ID will be converted to xid.ID:

func (id *ID) UnmarshalJSON(b []byte) error {
    s := string(b)
    if s == "null" {
        *id = nilID
        return nil
    }
    return id.UnmarshalText(b[1 : len(b)-1])
}

As you can see, the first byte is cut off, and ID.UnmarshalText() does even more "magic" on it (check the source if you're interested).

All-in-all, to avoid such "transformations" happen in the background without your knowledge, use a simple string type for your ID, and do necessary conversions yourself wherever you need to store / transmit your ID.

icza
  • 389,944
  • 63
  • 907
  • 827
0

For the ID Field, you should use the primitive.ObjectID provided by the bson package.

"go.mongodb.org/mongo-driver/bson/primitive"

ID          primitive.ObjectID `json:"_id" bson:"_id"`
Eduardo Hitek
  • 553
  • 2
  • 13