1

So I am currently trying to replace an array in an Element.

oldEvent.interests = ["test", "test2"];
console.log(oldEvent);

but my Element is not changed in the console.log. oldEvent.interests is an empty array and in the log it is still empty.

My model looks like this:

const eventSchema = new mongoose.Schema({
  title: {
    type: String, required: true, minlength: 2, maxlength: 128,
    validate: {
      validator: v => /^[\w\d öäüÖÄÜ]*$/.test(v),
      message: props => `"${props.value}" may only consist of normal letters and numbers`
    },
  },
  date: {type: Date, required: true},
  description: {type: String, required: true, maxlength: 999},
  maxParticipants: {type: Number, min: 0},
  participants: [{type: mongoose.Schema.Types.ObjectId, ref: "User"}],
  joinDeadline: {type: Date, required: true},
  interests: {
    type: [{type: mongoose.Schema.Types.ObjectId, ref: "Interest"}],
    validate: {
      validator: v => v.length <= 10,
      message: props => `"${props.value}" may only consist of up to 10 interests`
    },
  },
  host: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true},
  location: {type: geoSchema}
});

I don't want to pop the array empty and then readd the new elements, is there a more elegant way ?

6
  • Do you want to replace it in the database? In that case you can just update the document with the new array. If you want to update a local value in node.js, you can also set it directly. I'm not sure what your issue is, do you want to access the previous value before the update? Commented May 13, 2020 at 18:04
  • Eventually. I have a function that is run later to check whether the interests field is valid, then I use .save to save it again. (Theres also some other validation going on, including mongoose validation, thats why I want to replace the array) But the issue is, that setting the array, like in my example, does not change the oldEvent.interests-Field, in the console.log it still is empty. Commented May 13, 2020 at 20:09
  • 1
    You can use oldEvent.set({ interest: ["test", "test2"] }) and oldEvent.save() Commented May 13, 2020 at 20:33
  • Exactly what I was looking for, I don't know why = does not work. If you want you can for that into an answer ;:) Commented May 14, 2020 at 8:14
  • Edit: set only works if the array is empty, I was able to set it once, but then set also does not change the array. :( Commented May 14, 2020 at 8:21

2 Answers 2

4

If you have a mongoose document, you can modify the properties with document.set() . This will modify the local copy of your document.

Then you will have to call document.save() to update it in the database.

Note that what you set needs to match with your schema, otherwise mongoose will not update your document.

In your case you can do

oldEvent.set("interest", [id1, id2])
...
await oldEvent.save()

or

oldEvent.set({ interest: [id1, id2] })
...
await oldEvent.save()
Sign up to request clarification or add additional context in comments.

5 Comments

Looks like mongoose checks if the array I am trying to set consists of valid IDs, so your example does NOT work.
Thanks, it was actually an extra }
No that syntax is wrong, node shell and Webstorm don't accept that. It needs to be a object.
It's meant to be 2 separate parameters, I might have been staring too much at the screen, fixed now
Ah ok now it makes sense :D thanks for clarification
0

Because Interests is made of ObjectIDs it looks like Mongoose is already checking the elements of the array when trying to replace it. So my tests with invalid object ids won't change the array.

        // Won't work
        oldEvent.interests = ["asdflkashjk"];

        // Works
        oldEvent.interests = ["5eb40a55125a8e356c3bc716"];

Which is actually very good, as my main validation concern was to check if the ids are valid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.