/ / Mongoose findOneAndUpdateをObjectIdとして更新 - node.js、mongodb、mongoose

ObjectIdとして更新されるMongoose findOneAndUpdate - node.js、mongodb、mongoose

私は次のスキーマを持っています:

let PlayerSchema = new mongoose.Schema( {
name: String,
country: String,
roundResults: [ {
round: Number,
result: String,
points: Number
} ]
} );

私が使うとき findOneAndUpdate 文書を更新するには

Player.findOneAndUpdate({ id: req.params.id },
{$push: {"roundResults": result }},
{safe: true, upsert: true, new : true},
(err, player) => {
if( ! err && player ) {
res.json( player );
} else {
res.json( err );
}
}
);

私は次のようになる 予想外の 結果:

[
{
"_id": "57c9eb55c2a07a401462e3ec",
"name": "Joe Bloggs",
"country": "England",
"__v": 0,
"roundResults": [
{
"_id": "57c9eba11c597d5e1460e4f0"
}
]
}
]

私は roundResults スキーマが、私は得る _id 上記のように、私は見ることを期待するでしょう:

"roundResults": [
"round": 1,
"result": "1",
"points": 3
]

誰がこれがなぜであるのか、または私が真上に表示しているデータをネストするためにこれをどのように取得するのかを説明できますか?

回答:

回答№1は1

サブ文書にリテラルオブジェクトを渡しても、それは Schema。そして id そのために生成されます。配列の場合はその型を定義します Array.

プロパティにスキーマを定義した場合、そのスキーマはサブクラスおよびエンティティと見なされるため、そのスキーマはサブクラスおよびエンティティと見なされます。 id