Using shortid as custom Object ID in MongoDB

33 views Asked by At

Following up with Mongodb custom object id & Creating custom Object ID in MongoDB, I want to use shortid as custom Object ID in MongoDB. Here is my relevant Go code:

type Book struct {
    _id       string
    Title     string
    Author    string
    ISBN      string
    Publisher string
    Copies    int
}

. . .

    id := shortid.MustGenerate()
    book1 := Book{id, "Animal Farm", "George Orwell", "0451526341", "Signet Classics", 100}
    insertResult, err := booksCollection.InsertOne(context.TODO(), book1)

It should be the case that,

if some value is present in _id field of a document being stored, it is treated as objectId.

However, I found that my shortid is still being replaced by MongoDB:

enter image description here

So, is it still possible?
Is there anything that I've done wrong? thx

0

There are 0 answers