2017-04-20 206 views
0

以下代碼工作正常。但是,當我從我的cmd(節點服務器)再次運行它時,我得到了菜名的重複鍵信息。我有兩個文件。在dishes.js中,我定義了我的模式,並將其提供給我的第二個文件server.js。E11000重複密鑰錯誤集合:

// grab the things we need 
    var mongoose = require('mongoose'); 
    var Schema = mongoose.Schema; 

    var commentSchema = new Schema({ 
     rating: { 
      type: Number, 
      min: 1, 
      max: 5, 
      required: true 
     }, 
     comment: { 
      type: String, 
      required: true 
     }, 
     author: { 
      type: String, 
      required: true 
     } 
    }, { 
     timestamps: true 
    }); 
    // create a schema 
    var dishSchema = new Schema({ 
     name: { 
      type: String, 
      required: true, 
      unique: true 
     }, 
     description: { 
      type: String, 
      required: true 
     }, 
     comments:[commentSchema] 
    }, 
    { 
     timestamps: true 
    }); 

    // the schema is useless so far 
    // we need to create a model using it 
    var Dishes = mongoose.model('Dish', dishSchema); 

    // make this available to our Node applications 
    module.exports = Dishes; 

和我server.js文件。

  var mongoose = require('mongoose'), 
      assert = require('assert'); 

     var Dishes = require('./models/dishes-3'); 

     // Connection URL 
     var url = 'mongodb://localhost:27017/conFusion';mongoose.connect(url); 
     var db = mongoose.connection; 
     db.on('error', console.error.bind(console, 'connection error:')); 
     db.once('open', function() { 
      // we're connected! 
      console.log("Connected correctly to server"); 

      // create a new dish 
      Dishes.create({ 
       name: 'Uthapizza', 
       description: 'Test', 
       comments: [ 
        { 
         rating: 3, 
         comment: 'This is insane', 
         author: 'Matt Daemon' 
        } 
       ] 
      }, function (err, dish) { 
       if (err) throw err; 
       console.log('Dish created!'); 
       console.log(dish); 

       var id = dish._id; 

       // get all the dishes 
       setTimeout(function() { 
        Dishes.findByIdAndUpdate(id, { 
          $set: { 
           description: 'Updated Test' 
          } 
         }, { 
          new: true 
         }) 
         .exec(function (err, dish) { 
          if (err) throw err; 
          console.log('Updated Dish!'); 
          console.log(dish); 

          dish.comments.push({ 
           rating: 5, 
           comment: 'I\'m getting a sinking feeling!', 
           author: 'Leonardo di Carpaccio' 
          }); 

          dish.save(function (err, dish) { 
           console.log('Updated Comments!'); 
           console.log(dish); 

           db.collection('dishes').drop(function() { 
            db.close(); 
           }); 
          }); 
         }); 
       }, 3000); 
      }); 
     }); 

如果你支付我已刪除了獨特的server.js文件中的密切關注:從dishes.js文件真實屬性,但我仍然有同樣的問題。

name: { 
     type: String, 
     required: true, 
     unique: true 
    }, 
+0

嘗試刪除收集並重新啓動服務器 –

+0

我正在刪除它。 db.collection('dishes')。drop(function(){ db.close(); }); – Theo

+0

但沒有任何反應。 – Theo

回答

0

當你的模式如下

name: { 
     type: String, 
     required: true, 
     unique: true 
    } 

唯一給出的工作

當你的模式如下

name: { 
     type: String, 
     required: true 
    } 

獨特的工作不

後給出呃改變你的模式定義,刪除所有的集合並嘗試插入。