-
import mongoose from "mongoose";
import util from 'util';
import { config } from '../../config';
mongoose.set('debug', true);
const Model = mongoose.model('Model', new mongoose.Schema({
values: [{ key: String }]
}));
(async function main() {
try {
await mongoose.connect(config.MONGODB_URI);
// seed
const model = await Model.create({ _id: new mongoose.Types.ObjectId(), values: [{ key: 'v1' }, { key: 'v2' }] })
const doc = await Model.findByIdAndUpdate(model._id, { values: { key: 'v3' } }, { new: true });
console.log(util.inspect(doc, false, null))
} catch (error) {
console.error(error);
} finally {
// await Promise.all(['models'].map(c => mongoose.connection.dropCollection(c)))
await mongoose.connection.close()
}
})(); Logs: Mongoose: models.findOneAndUpdate({ _id: ObjectId("64706425de19cdc0b8f49291") }, { '$set': { values: [ { key: 'v3', _id: ObjectId("64706426de19cdc0b8f49296") } ] }}, { returnDocument: 'after', returnOriginal: false })
{
_id: new ObjectId("64706425de19cdc0b8f49291"),
values: [ { key: 'v3', _id: new ObjectId("64706426de19cdc0b8f49296") } ],
__v: 0
} As you can see, mongoose wrapped the I debug step by step but didn't find the reason. Please explain, thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
vkarpov15
May 26, 2023
Replies: 1 comment
-
Your schema defines |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mrdulin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your schema defines
values
as an array, so Mongoose casts the value in your update to an array.