Replies: 2 comments 3 replies
-
|
Here's an extremely simplified repro. The idea is that Mongoose returns The most likely explanation is that there's some sort of issue where Mongoose doesn't think the field you're trying to update is in your schema. Try executing your update with |
Beta Was this translation helpful? Give feedback.
-
|
That makes sense, I'm sorry for the trouble. For what it is worth, the code suggested in https://stackoverflow.com/questions/38248365/mongoose-schema-with-nested-optional-object/38250186#38250186 never actually worked as you might expect. In Mongoose 5 and older, the following schema would have {
data1: String,
nested: {
type: {
nestedProp1: String,
nestedSub: [String]
},
required: false
}
}As of Mongoose 6, there shouldn't be anything wrong with setting {
data1: String,
nested: {
type: new Schema({
nestedProp1: String,
nestedSub: [String]
}),
required: false
}
}Do you have any more info on what your schema looks like? The fact that you're seeing this issue could be indicative of a update casting issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
After uppgrading to Mongoose 6, an
updateOne()statement started to return{ acknowledged: false }.No logging is generated to explain /why/ it was not acknowledged, which would be super-useful. The query is not sent to MongoDB.
The Mongoose docs say a positive acknowledgement means "everything meant smoothly". This is not helpful enough to know what the cause is when things don't go smoothly.
I eventually tracked the issue down to some code we wrote in 2017 which required an optional sub-document. At that time, multiple StackOverflow posts from the era recommended using a
typekey in the schema that points to a POJO. Here's one such post:https://stackoverflow.com/questions/38248365/mongoose-schema-with-nested-optional-object/38250186#38250186
Mongoose changed related behavior in Mongoose 6, which is referenced in this issue:
#8107
There's a note in the migration guide here:
https://mongoosejs.com/docs/6.x/docs/migrating_to_6.html#typepojotomixed
But the migration guide note make it sounds like the old schemas would keep working, but they do not. Rather than have a migration note, I would rather that Mongoose just crash at schema creation type with a note that such schema definitions where
typepoints to a POJO like this are no longer supported. The code could only exist in the Mongoose 6 branch.Otherwise, I recommend updating the migration guide:
Repro code
Code like this works in Mongoose 5, but fails in Mongoose 6 until you remove the
type:wrapper object.When running the code, look for whether
acknowledged:trueoracknowledged:falseis returned in the result.Beta Was this translation helpful? Give feedback.
All reactions