3

I am very new to both Go and Mongodb and was writing my first rest-api with Go and Mongo. I am using mongo-go-driver and have the following Modal struct in Go

type Modal struct {
    Group     []string           `bson:"group" json:"group"`
    Hostname  string             `bson:"hostname" json:"hostname"`
    Overrides map[string]string  `bson:"overrides" json:"overrides"`
    Excludes  []string           `bson:"excludes" json:"excludes"`
}

I do not want to use the default ObjectId field provided by mongo-db as my primary key and instead would like to make the Hostname field as the primary key.

If I make the type of Hostname field as primitive.ObjectID, then the hostname would be unique but its value will be randomly generated string by mongodb and not the actual hostname string value.

So is there a way I can do this.

icza
  • 389,944
  • 63
  • 907
  • 827
Rohit
  • 3,659
  • 3
  • 35
  • 57
  • I do not think this is a golang related question at all tho. And you are applying relational db primary key notions to a non relational db. A primary key is just an identifier, data could, and often will, be redundant in that kind of database. – Andrea Golin Apr 30 '19 at 12:44
  • @AndreaGolin i used the word _primaryKey_ just to highlight the fact that column to have unique data. This is a `go` related because the `mongo` db driver in question is for `go`. – Rohit Apr 30 '19 at 12:52
  • I think it is a Golang related question, since in Javascript you can use the mongoose driver to enforce unique constraints, required constraints, etc. and I was wondering how to do this in Go. – Taimoor Ahmad Jan 01 '21 at 18:10

1 Answers1

11

You may use a unique index to enforce / allow only distinct values of a given field, e.g.:

db.collectionname.createIndex( { "hostname": 1 }, { unique: true } )

If you want to create such index using the official MongoDB driver, this is how you can do that:

indexName, err := coll.Indexes().CreateOne(
    context.Background(),
    mongo.IndexModel{
        Keys:    bson.D{{Key: "hostname", Value: 1}},
        Options: options.Index().SetUnique(true),
    },
)

But know that in MongoDB each document must have an _id property, so doing the above, documents will have an auto-generated _id field (of ObjectId type). If this doesn't bother you, you're done.

Also note that you may map Modal.Hostname to the _id field with struct tags:

type Modal struct {
    Group     []string           `bson:"group" json:"group"`
    Hostname  string             `bson:"_id" json:"hostname"`
    Overrides map[string]string  `bson:"overrides" json:"overrides"`
    Excludes  []string           `bson:"excludes" json:"excludes"`
}

And again, you're done. The downside of this solution is that the documents in MongoDB will not have a property named hostname, as it will be stored in _id.

icza
  • 389,944
  • 63
  • 907
  • 827
  • thanks for the reply, i thought about the first solution but the problem was I have to do that from `mongo` shell. – Rohit Apr 30 '19 at 12:57
  • @Rohit You only have to create the index once, why is it a problem to access the shell? By the way, you can create indices from a Go app too. – icza Apr 30 '19 at 12:58
  • accessing shell is not a problem, its just that I wanted to do that from Go application, infact that is wanted to ask in first place, sorry if my question was not clear. Can you please update your answer to let me know how can I do that. – Rohit Apr 30 '19 at 13:01
  • @Rohit The "how" depends on what Go driver you're using. – icza Apr 30 '19 at 13:06
  • its `mongo-go-driver` – Rohit Apr 30 '19 at 13:07
  • @Rohit Added example code to create a unique index. – icza Apr 30 '19 at 13:29
  • Thank you very much, exactly what I needed !! – Rohit Apr 30 '19 at 13:31