このスキーマを次のように作成しましたmongoose Schema
:
socialAccount = new Schema({
socialNetwork : { type : String , required : true},
userid : { type : Number, required : true },
username : String
},{_id : false});
person = new Schema({
id : { type : Number, unique : true, required : true , dropDups : true },
firstname : String,
lastname : String,
socialAccounts : [socialAccount],
updated : { type : Date, default : Date.now },
enable : { type : Boolean , default : true },
});
メソッドでデータを取得するとfindOne
、結果は次のようになります(in console.log()
):
{
id: 1,
firstname: 'example name',
lastname: 'example last',
enable: true,
updated: Thu Sep 24 2015 09:40:17 GMT+0330 (IRST),
socialAccounts:
[ { socialNetwork: 'instagram',
userid: 1234567,
username: 'example' } ] }
SO、ループ構造を使用してサブドキュメントを反復処理し、データを表示するとsocialAccounts
、他のオブジェクトと関数が返され、最初のものだけがサブドキュメント オブジェクトになります。この for ループ iterate メソッドを使用して、サブドキュメント
の最初の要素のみを取得するにはどうすればよいですか。for var in
console.log()
socialAccounts
ありがとう