【问题标题】:How to make unique identifier with 2 keys in mongoose? [duplicate]如何在猫鼬中使用 2 个键制作唯一标识符? [复制]
【发布时间】:2016-11-25 13:56:32
【问题描述】:

我不知道如何在标题中准确解释我的问题。很抱歉。

这就是我的意思: 我有用户模型

var userSchema = new Schema({
    username: { type: String, required: true },
    messages: { type: Number, required: true, default: 0 },
    noticed: { type: Date, required: true, default: Date.now },
    chatId: { type: Number, required: true }
    })

usernamechatId 可能不是唯一的,但应该没有usernamechatId 相同的用户!

这意味着在一个聊天中不能有两个用户具有相同的用户名。但是用户可能在许多不同的聊天中。 我如何在猫鼬中做到这一点?

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    我认为这会奏效:

    userSchema.index({ username : 1, chatId : 1 }, { unique : true });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 2021-02-18
      • 2012-09-23
      相关资源
      最近更新 更多