2017-04-20 9 views
0

次のコードは正常に動作します。しかし、cmd(ノードサーバー)から再度実行すると、料理名の重複したキーメッセージが表示されます。私は2つのファイルを持っています。私が自分のスキーマを定義し、server.jsという2番目のファイルで利用できるようにするのは、dishes.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 
    } 

ユニークな作業ではない

後方の下に与えられたとき、あなたのスキーマは

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

独特の下に与えられたときあなたのスキーマ定義を変更し、すべてのコレクションを削除して挿入しようとします。

関連する問題