I'm having an issue using projection, particularly the $unset
field. I also had the same issue with $pull
with arrays. It seems like mongoose uses power-assign under the hood for async for-each operations.
export type IUserModel = Model<IUserDocument>
const UserSchema = new Schema<IUserDocument>({
email: { type: String },
meta: {
created: String,
updated: String,
},
photos: {
type: Map,
of: {
link: String,
size: Number,
}
},
// }, {timestamps: {createdAt: 'meta.created', updatedAt: 'meta.updated' } });
}, { timestamps: true });
export const User = model<IUserDocument, IUserModel>('users', UserSchema);
const removePhoto = async (id, key) => {
try {
const query = await User.updateOne({ _id: '' }, { $unset: { [`photos.${key}`]: true } })
console.log(query)
} catch(err) {
console.error(err)
}
}
I receive the following error:
*** uncaughtException: Error: Invalid operator: "updatedAt"
at Function.assignUpdateOperation (F:DESKTOPPROJECTSserver
ode_modulespower-assigndistpower-assign.js:125:21)
at F:DESKTOPPROJECTSserver
ode_modulespower-assigndistpower-assign.js:535:24
at Array.reduce (<anonymous>)
at assign (F:DESKTOPPROJECTSserver
ode_modulespower-assigndistpower-assign.js:534:15)
at saveDiffHistory (F:DESKTOPPROJECTSserver
ode_modulesmongoose-audit-trailindex.js:106:5)
at F:DESKTOPPROJECTSserver
ode_modulesmongoose-audit-trailindex.js:118:26
at handleNextResult (F:DESKTOPPROJECTSserver
ode_modulesmongooselibhelperscursoreachAsync.js:86:21)
at F:DESKTOPPROJECTSserver
ode_modulesmongooselibhelperscursoreachAsync.js:69:9
at F:DESKTOPPROJECTSserver
ode_modulesmongooselibcursorQueryCursor.js:460:7
at F:DESKTOPPROJECTSserver
ode_moduleskareemindex.js:136:16
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…