在Mongoose中为对象数组创建索引 [英] Create index in Mongoose for array of objects

查看:73
本文介绍了在Mongoose中为对象数组创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型设置:

var ShopsSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  }
});

var ShopsModel = mongoose.model("Shops", ShopsSchema);

FieldGroupsModel

var FieldGroupsSchema = new mongoose.Schema({
  title: {
    type: String,
    required: true
  },
  fields: [{
    label: {
      type: String,
      required: true
    },
    handle: {
      type: String,
      required: true
    }
  }],
  shop: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Shops"
  }
});

var FieldGroupsModel = mongoose.model("FieldGroups", FieldGroupsSchema)

每个FieldGroups实例都有一个与之关联的ShopsModel.

Each FieldGroups instance has a ShopsModel associated with it.

我需要为FieldGroupsModel fields [i] .handle 值创建一个索引,该索引需要两个规则;它对于每个FieldGroupsModel实例都必须是唯一的,因此此数据将无效:

I need to create an index for the FieldGroupsModel fields[i].handle value, this index needs two rules; it needs to be unique to each FieldGroupsModel instance, so this data would be invalid:

{
  title: "Some field group title here",
  fields: [
    {
      label: "Some label 1"
      handle: "some-label-1"
    },
    {
      label: "Some label 1"
      handle: "some-label-1" // Error: `some-label-1` already exists as a value of `fields[i].handle`.
    }
  ],
  shop: {
    "$oid": "1"
  }
}

第二条规则是,仅对于共享相同 shop 值的FieldGroupsModel实例,应保留第一条规则.因此,这些数据将无效:

The second rule is that the first rule should only be in place for FieldGroupsModel instances which share the same shop value. So this data would be invalid:

// First bit of data
{
  title: "Some field group title here",
  fields: [
    {
      label: "Some label 1"
      handle: "some-label-1"
    }
  ],
  shop: {
    "$oid": "1"
  }
}

// Second bit of data
{
  title: "Another field group title here",
  fields: [
    {
      label: "Some label 1"
      handle: "some-label-1" // Error: `some-label-1` already exists as a value of `fields[i].handle` of a document which shares the same `shop` value.
    }
  ],
  shop: {
    "$oid": "1"
  }
}

但是,这将是有效的:

// First bit of data
{
  title: "Some field group title here",
  fields: [
    {
      label: "Some label 1"
      handle: "some-label-1"
    }
  ],
  shop: {
    "$oid": "1"
  }
}

// Second bit of data
{
  title: "Another field group title here",
  fields: [
    {
      label: "Some label 1"
      handle: "some-label-1" // This is valid because there's no other documents with the same `shop` value with the same `fields[i].handle` value.
    }
  ],
  shop: {
    "$oid": "2"
  }
}

我对Mongo和Mongoose还是很陌生,因此对我们的任何帮助将不胜感激!!:)

I'm quite new to Mongo and Mongoose so any help here would be greatly appreciated! :)

推荐答案

您可以在Schema对象上调用 index 方法来执行此操作,如下所示

You call the index method on your Schema object to do that as shown here. For your case it would be something like:

FieldGroupsSchema.index({"shop": 1, "fields.handle": 1}, {unique: true});

有关详细信息,请阅读有关化合物索引的MongoDB文档.

Please read the MongoDB documentation about Compound Indexes for more detail.

这篇关于在Mongoose中为对象数组创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆