Home  >  Article  >  Backend Development  >  How to create unique pair index for Mongodb?

How to create unique pair index for Mongodb?

PHPz
PHPzforward
2024-02-10 17:00:101073browse

How to create unique pair index for Mongodb?

php editor Xigua will introduce you how to create a unique pair index for Mongodb. Mongodb is a non-relational database, and the pair index is a special index type used to ensure the uniqueness of document pairs in the collection. To create a unique pair index, you need to use Mongodb's createIndex method and specify the fields of the index as well as the uniqueness option. By correctly setting indexes, you can effectively avoid the insertion of duplicate data and improve data consistency and accuracy. Next, let’s take a look at the specific steps!

Question content

I am using mongodb and I want to make a pair unique on 2 fields.

Here's what I've done so far:

func (repository *translationrepository) createindexes(collection *mongo.collection) error {
    models := []mongo.indexmodel{
        {
            keys:    bson.d{{"object_id", 1}, {"object_type", 1}},
            options: options.index().setunique(true),
        },
        {
            keys:    bson.d{{"expire_at", 1}},
            options: options.index().setexpireafterseconds(0),
        },
    }

    opts := options.createindexes().setmaxtime(10 * time.second)
    _, err := collection.indexes().createmany(context.background(), models, opts)
    return err
}

But when I insert 2 records like this

{
    "object_id"  : "abc",
    "object_type": "sample" 
}

{
    "object_id"  : "edf",
    "object_type": "sample" 
}

There is only 1 record in the database

{
    "object_id"  : "edf",
    "object_type": "sample" 
}

The second one has overwritten the first one

The following is my sample code for inserting records

TranslationForm := entity.TranslationForm{
        ObjectID:       "ABC",
        ObjectType:     "SAMPLE",
        SourceLanguage: "en",
        TargetLanguage: "cn",
        Content:        "something",
        ExpireAt:       time.Now(),
    }
res, err := repository.collection.InsertOne(context.TODO(), TranslationForm)

Solution

I should manage your scenario. Let me share a simple program to show what I achieved.

package main

import (
    "context"
    "fmt"
    "time"

    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/mongo"
    "go.mongodb.org/mongo-driver/mongo/options"
)

type Object struct {
    ObjectId   string `json:"object_id" bson:"object_id"`
    ObjectType string `json:"object_type" bson:"object_type"`
}

func main() {
    ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*10)
    defer cancelFunc()

    clientOptions := options.Client().ApplyURI("mongodb://root:root@localhost:27017")
    mongoClient, err := mongo.Connect(ctx, clientOptions)
    if err != nil {
        panic(err)
    }
    defer mongoClient.Disconnect(ctx)

    demoDb := mongoClient.Database("demodb")
    defer demoDb.Drop(ctx)
    myCollection := demoDb.Collection("myCollection")
    defer myCollection.Drop(ctx)

    // create index
    indexModel := mongo.IndexModel{
        Keys: bson.D{
            bson.E{
                Key:   "object_id",
                Value: 1,
            },
            bson.E{
                Key:   "object_type",
                Value: 1,
            },
        },
        Options: options.Index().SetUnique(true),
    }
    idxName, err := myCollection.Indexes().CreateOne(ctx, indexModel)
    if err != nil {
        panic(err)
    }

    fmt.Println("index name:", idxName)

    // delete documents
    defer func() {
        if _, err := myCollection.DeleteMany(ctx, bson.M{}); err != nil {
            panic(err)
        }
    }()

    // insert first doc
    res, err := myCollection.InsertOne(ctx, Object{ObjectId: "abc", ObjectType: "SAMPLE"})
    if err != nil {
        panic(err)
    }
    fmt.Println(res.InsertedID)

    // insert second doc
    // res, err = myCollection.InsertOne(ctx, Object{ObjectId: "abc", ObjectType: "SAMPLE"}) => ERROR
    res, err = myCollection.InsertOne(ctx, Object{ObjectId: "def", ObjectType: "SAMPLE"}) // => OK!
    if err != nil {
        panic(err)
    }
    fmt.Println(res.InsertedID)

    // list all docs
    var objects []Object
    cursor, err := myCollection.Find(ctx, bson.M{})
    if err != nil {
        panic(err)
    }
    if err = cursor.All(ctx, &objects); err != nil {
        panic(err)
    }
    fmt.Println(objects)
}

Now, I'll go over all the major steps:

  1. object Definition of the structure, this is a simplified version of what you need. Please note the actual use of bson comments. For the sake of this demonstration, you can safely omit json.
  2. Settings related to the mongo ecosystem:
    1. Context creation (with timeout)
    2. Client settings (connect to local mongodb instance running via docker)
    3. Create a database named demodb and a collection named mycollection. Also, I defer the call to delete these when exiting the program (just to clean up).
  3. Create a unique composite index on fields object_id and object_type. Note the options field, which declares the uniqueness of the index using the setunique method.
  4. Add documentation. Please note that the program does not allow you to insert two documents with the same fields. You can try commenting/uncommenting these cases to confirm again.
  5. For debugging purposes, I ended up listing the documents in the collection to check if the second document was added.

I hope this demo answers some of your questions. Let me know and thanks!

The above is the detailed content of How to create unique pair index for Mongodb?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete