MongoDB子文档和人口参考 - “E11000重复密钥错误集合”

时间:2016-04-17 15:57:12

标签: node.js mongodb

我有一个名为电子商务的MongoDB库,其中包含产品用户购物车的集合,以及订单。我仍然在构建一些端点,但是现在我遇到了一个我似乎无法解决的错误。

当我尝试添加用户时,响应显示:“E11000重复键错误集合:ecommerce.users index:product.title_1 dup key:{:null}”

用户模型 - 请注意购物车使用 CartSchema

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var cartSchema = require('./cartSchema');

var userSchema = new Schema({
  username: { type: String, lowercase: true, required: true },
  email: {type: String, lowercase: true, unique: true, index: true },
  password: { type: String, required: true },
  cart: [cartSchema],
  orders: []
});

module.exports = mongoose.model('User', userSchema);

购物车架构 - 请注意产品使用参考产品模型

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var Product = require('./productModel');

module.exports = new Schema({
  products: [{
    product: { type: Schema.Types.ObjectId, ref: 'Product', required: true },
    quantity: { type: Number, min: 1 }
  }],
});

产品型号

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var productSchema = new Schema({
  title: {
    type: String,
    require: true,
    unique: true,
    index: true
  },
  description: {
    type: String,
    required: true
  },
  price: {
    type: Number,
    required: true,
    min: 0
  }
})

module.exports = mongoose.model('Product', productSchema)

我目前在数据库中拥有的产品(注意每个产品都有一个独特的标题):

/* 1 */
{
    "_id" : ObjectId("57104fdfa2c2256e45744706"),
    "title" : "Booze",
    "description" : "Goes down smooth",
    "price" : 16,
    "__v" : 0
}

/* 2 */
{
    "_id" : ObjectId("57105038f560607f45b05888"),
    "title" : "Woody",
    "description" : "Sheriff over toys",
    "price" : 5,
    "__v" : 0
}

/* 3 */
{
    "_id" : ObjectId("571181a12df983d208f4600b"),
    "title" : "Heart",
    "description" : "The love of your life",
    "price" : 68,
    "__v" : 0
}

第一个用户保存得很好:

{
    "username" : "hman",
    "password" : "henry123456",
    "email" : "henry@gmail.com",
    "cart" : {
        "products": [{
            "product" : "57104fdfa2c2256e45744706",
            "quantity" : 1
        }]
    }
}

当我尝试保存第二个用户时......

{
    "username" : "toystorylover95",
    "password" : "woody123",
    "email" : "woody@toystory.com",
    "cart" : {
        "products": [{
            "product" : "57105038f560607f45b05888",
            "quantity" : 20
        }]
    }
}

...我收到了错误:

{
  "code": 11000,
  "index": 0,
  "errmsg": "E11000 duplicate key error collection: ecommerce.users index: product.title_1 dup key: { : null }",
  "op": {
    "username": "woody-wood",
    "password": "woody123",
    "email": "woody@woody.com",
    "_id": "5713af5efd626d381700437f",
    "orders": [],
    "cart": [
      {
        "_id": "5713af5efd626d3817004380",
        "products": [
          {
            "product": "57105038f560607f45b05888",
            "quantity": 20,
            "_id": "5713af5efd626d3817004381"
          }
        ]
      }
    ],
    "__v": 0
  }
}

我做错了什么?

这是在/ users端点调用的函数,如果它有帮助:

function(req, res) {
  User.create(req.body, function(err, result) {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).send(result);
    }
  })
}

0 个答案:

没有答案