doulai7239 2017-05-23 12:28
浏览 36
已采纳

如何使用mgo在单个集合中处理多种类型

I'm very new to golang and try to write a simple event-sourcing user-management webapi using mongodb as backing database. Now i have User, which looks something like this:

type User struct {
Id       bson.ObjectId `json:"id" bson:"_id"`
UserName string        `json:"username" bson:"username"`
Email    string        `json:"email" bson:"email"`
PwdHash  string        `json:"pwd_hash" bson:"pwd_hash"`
FullName string        `json:"fullname" bson:"fullname"`
}

and three events, happening to user, when somebody uses api:

type UserCreatedEvent struct {
    UserId         bson.ObjectId `json:"id" bson:"_id"`
    //time when event was issued
    CreatedEpoch   time.Time     `json:"created_epoch" bson:"created_epoch"`
    //id of user that made a change
    IssuedByUserId bson.ObjectId `json:"issuedby_userid" bson:"issuedby_userid"`
}

type UserDeletedEvent struct {
    UserId         bson.ObjectId `json:"id" bson:"_id"`
    CreatedEpoch   time.Time     `json:"created_epoch" bson:"created_epoch"`
    //id of user that made a change
    IssuedByUserId bson.ObjectId `json:"issuedby_userid" bson:"issuedby_userid"`
}

type UserUpdatedEvent struct {
    UserId         bson.ObjectId `json:"id" bson:"_id"`
    CreatedEpoch   time.Time     `json:"created_epoch" bson:"created_epoch"`
    //id of user that made a change
    IssuedByUserId bson.ObjectId `json:"issuedby_userid" bson:"issuedby_userid"`
    ChangedFieldName     string  `json:"changed_field_name" bson:"changed_field_name"`
    NewChangedFieldValue string  `json:"new_changed_field_value" bson:"new_changed_field_value"`
}

Now i'm stuck on saving and retrieving events from db. The problem is i want to store them in a single collection, so that i have a full plain history of user modifications. But i can't find how to correctly store event type name as a mongo document field and then use it in searches. What is the idiomatic go-way to do this?

I'll be gratefull for any help.

  • 写回答

1条回答 默认 最新

  • dongweicha6077 2017-05-23 13:35
    关注

    What's nice about a non-relational database is that a little redundancy is OK and is faster for retrieval since you're not joining things together. You could just add your bottom three objects as properties on your User in whatever fashion makes sense for you and your data. Then you wouldn't need to store the UserId on those objects either.

    If you need to search over the Events quickly, you could create another collection to hold them. You'd be inserting to two collections, but retrieval times/logic should be pretty good/easy to write.

    Your new Event would be something like:

    type UserEventType int
    
    const (
        Created UserEventType = iota
        Deleted
        Updated
    )
    
    type UserEvent struct {
        UserId               bson.ObjectId `json:"id" bson:"_id"`
        CreatedEpoch         time.Time     `json:"created_epoch" bson:"created_epoch"`
        //id of user that made a change
        IssuedByUserId       bson.ObjectId `json:"issuedby_userid" bson:"issuedby_userid"`
        ChangedFieldName     string        `json:"changed_field_name,omitempty" bson:"changed_field_name,omitempty"`
        NewChangedFieldValue string     `json:"new_changed_field_value,omitempty" bson:"new_changed_field_value,omitempty"`
        EventType            UserEventType `json:"user_event_type" bson:"user_event_type"`
    }
    

    Notice the omitempty on the fields that are optional depending on the type of event.

    You really aren't supposed to store different objects in the same collection. Here's a line from the Mongo documentation:

    MongoDB stores documents in collections. Collections are analogous to tables in relational databases.

    In case you aren't familiar with relational databases, a table would essentially represent one type of object.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记