2017-04-04 6 views
2

mongoデータベースへのデータの挿入リクエストを投稿しようとしています。私はこれにmongooseを使用しています。私は以下のようなmongooseSchemaを作成している -body-parserを使用してnodejsとmongooseを使用して、ネストされたオブジェクトの配列をmongodbデータベースに追加/追加する方法

const countrySchema = mongoose.Schema({ 
    country_id: { 
     type : Number, 
     required: true, 
     unique : true 
    }, 
    country_name : { 
     type : String, 
     required : true, 
     unique : true 
    }, 
    country_description : { 
     type : String, 
     required : true, 
     unique : true 
    }, 
    country_currency : { 
     type : String, 
     required : true, 
     unique : true 
    }, 
    country_capital : { 
     type : String, 
     required : true, 
     unique : true 
    }, 
    country_numberof_districs : { 
     type : Number, 
     required : true 
    }, 
    country_createdat : { 
     type : Date, 
     required : true 
    }, 
    country_districs : { 
     districs : [ 
     { 
      distric_id : { 
      type : Number, 
      // required : true, 
      }, 
      distric_name : { 
      type : String, 
      // required : true, 
      }, 
      distric_description : { 
      type : String, 
      // required : true, 
      }, 
      distric_famous_for : { 
      type : String, 
      // required : true 
      } 
     } 
     ] 
    } 
}); 

以下は、私のpostメソッドである -

router.post('/addcountry', (request, response, next) => { 

    const district = []; 
    district.push({ 
    distric_id: request.body.distric_id, 
    distric_name : request.body.distric_name, 
    distric_description : request.body.distric_description, 
    distric_famous_for : request.body.distric_famous_for 
    }); 


    let newCountry = new country({ 
    country_id : request.body.country_id, 
    country_name : request.body.country_name, 
    country_description : request.body.country_description, 
    country_currency : request.body.country_currency, 
    country_capital : request.body.country_capital, 
    country_numberof_districs : request.body.country_numberof_districs, 
    country_createdat : new Date(), 
    country_districs : { 
     districs : [district] 
    } 
    }); 
    country.addCountry(newCountry, (err, country) =>{ 
    console.log('came here'); 
    if(err){ 
     response.json({ 
     success : false, 
     message : 'failed to add country' + err 
     }); 
    }else { 
     response.json({ 
     success : true, 
     message : 'country added successfully' 
     }); 
    } 
    }); 
}); 

私はPostmanでテスト。 districsの部分がなくても正常に動作します。

{ 
    "country_id" : 909773, 
    "country_name" : "USA", 
    "country_description" : "UNITED STATRE", 
    "country_currency" : "USD", 
    "country_capital" : "NEWYORK", 
    "country_numberof_districs" : 100, 
    "country_districs": { 
     "districs": 
     [ 
      { 
       "distric_id": 777, 
       "distric_name" : "melbourne", 
       "distric_description" : "no des", 
       "distric_famous_for" : "beaches" 
      }, 
      { 
       "distric_id": 900, 
       "distric_name" : "sydney", 
       "distric_description" : "no des", 
       "distric_famous_for" : "beaches" 
      } 
     ] 
    } 
} 

結果はこのようなものだった - - 私は、オブジェクトの下に投稿しようとした

{ 
    "_id" : ObjectId("58e37b5c3c6b07153600a7f6"), 
    "country_id" : 909773, 
    "country_name" : "USA", 
    "country_description" : "UNITED STATRE", 
    "country_currency" : "USD", 
    "country_capital" : "NEWYORK", 
    "country_numberof_districs" : 100, 
    "country_createdat" : ISODate("2017-04-04T10:54:20.726Z"), 
    "country_districs" : { 
     "districs" : [ 
      { 
       "_id" : ObjectId("58e37b5c3c6b07153600a7f7") 
      } 
     ] 
    }, 
    "__v" : 0 
} 

:まず、私は1つの地区で試してみました、それもうまくいきませんでした。このディストリクトオブジェクトを追加するにはどうすればいいですか(ディストリクトアレイのように多くの場合があります)、すべてのディストリクトオブジェクトをどのように反復することができますか。これであなたの助けを願ってください。フィールドの残りの部分と一緒に

let newCountry = new country({ 
    country_districs : { 
     districs : req.body.country_districs.districs 
    } 
}); 

国を作りながら

+1

あなたはこれを試すことができます... - - '{(newCountry =新しい国を聞かせて国を作りながら –

+0

もし 'districtric'キーがあなたが本当に望むものでなければ、単に配列を保存することができますスキーマ内の変数を配列として定義してオブジェクトのオブジェクトの配列を対応するキーに格納することができます。 したい場合は、私は地区があなたが country_districs変更してみてください –

+0

のための必須要件ではない場合にのみ、簡単な例を投稿することができます:{ districs:地区 } –

答えて

1

は、あなたはこれを試すことができます country_districs:{ districts:req.body.country_districts.districts } });残りのフィールドと一緒に...