Matt Webb Matt Webb - 1 year ago 422
Node.js Question

Mongoose findOneAndUpdate updating as ObjectId

I have the following schema:

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

When I use
to update a document:

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

I get the following unexpected result:

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

I don't have a
schema but, I get an
as shown above, I would expect to see,:

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

Can anyone explain why this is, or how I get this to nest the data I am displaying directly above?

Answer Source

Even if you pass literal object for sub documents, it is a Schema. And id would be generated for that. If it is an array define its type Array.

Attention that if you define any schema for a property it is considered as a subdoc and entity so it has id

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download