Replies: 2 comments 3 replies
-
Here's an extremely simplified repro. The idea is that Mongoose returns const mongoose = require('mongoose');
async function main() {
await mongoose.connect('mongodb://localhost/test');
const UserSchema = new mongoose.Schema({
integrations: {
mine: {
required: false,
select: false,
algorithm: {
type: String,
enum: ['first','second'],
},
type: {
credentials: {
access_token: { type: String },
refresh_token: { type: String },
expires_at: { type: Number },
},
lastDataDate: { type: 'Date', default: null },
profile: {
id: { type: Number },
},
recentNotifications: [
{
notificationDate: { type: Date, required: true },
externalId: { type: Number, required: false },
tripDetected: { type: Boolean, required: true },
logs: { type: [String], required: true },
},
],
}
}
}
});
const User = mongoose.model('User', UserSchema);
const res = await User.updateOne({ _id: '66fead1406c6a2afa4a6953e' }, {});
console.log('result was:', res)
}
main(); 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 const res = await User.updateOne({ _id: '66fead1406c6a2afa4a6953e' }, { $push: { ... } }, { strict: 'throw' });
console.log('result was:', res) |
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.
-
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
type
key 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
type
points 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:true
oracknowledged:false
is returned in the result.Beta Was this translation helpful? Give feedback.
All reactions