小编Bra*_*ick的帖子

Mongo在非唯一字段上给出"重复键错误"

我在尝试插入子文档时收到MongoDB错误.subdocs已经具有唯一的_id,但是对于我不想要唯一的不同的非唯一字段,会抛出错误.

Angular中的错误是:"Assets.serial已经存在". 如何使此字段包含重复值,以及导致模型假设它应该是唯一的?

这是我的Mongoose模型:

'use strict';

var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var AssetUrlSchema = new Schema({
  name: {
    type: String,
    unique: false,
    default: '',
    trim: true
  },
  url: {
    type: String,
    unique: false,
    default: 'http://placehold.it/75x75',
    trim: true
  },
}),

AssetSchema = new Schema({
  serial: {
    type: Number,
    unique: false
  },
  urls: {
    type: [AssetUrlSchema],
    unique: false,
    default: [
      { name: '', url: 'http://placehold.it/75x75' },
      { name: '', url: 'http://placehold.it/75x75' }
    ]
  }
}),

/**
 * Item Schema
 */ …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js angularjs

7
推荐指数
1
解决办法
2781
查看次数

标签 统计

angularjs ×1

mongodb ×1

mongoose ×1

node.js ×1