meteor-collection2 icon indicating copy to clipboard operation
meteor-collection2 copied to clipboard

Question: How can I delete an empty array from database?

Open juanpmd opened this issue 7 months ago • 3 comments

I currently have a schema definition like this:

const PeopleSchema = new SimpleSchema({
  userId: { type: String },
  interests: { type: Array, optional: true },
  'interests.$': { type: String, optional: true },
});

If the interests field comes with values, it should save the array but if the interests field is an empty array, it should not store it in the database (remove the field completely). I do this with strings perfectly but I cant get it to work with Arrays. Does anyone know how I could do this?

juanpmd avatar Apr 24 '25 18:04 juanpmd

Thank you for submitting this issue!

We, the Members of Meteor Community Packages take every issue seriously. Our goal is to provide long-term lifecycles for packages and keep up with the newest changes in Meteor and the overall NodeJs/JavaScript ecosystem.

However, we contribute to these packages mostly in our free time. Therefore, we can't guarantee you issues to be solved within certain time.

If you think this issue is trivial to solve, don't hesitate to submit a pull request, too! We will accompany you in the process with reviews and hints on how to get development set up.

Please also consider sponsoring the maintainers of the package. If you don't know who is currently maintaining this package, just leave a comment and we'll let you know

github-actions[bot] avatar Apr 24 '25 18:04 github-actions[bot]

@juanpmd this should be solvable using minCount set to 1 (see minCount/maxCount) when interests is set:

const PeopleSchema = new SimpleSchema({
  userId: { type: String },
  interests: { 
    type: Array, 
    optional: true, 
    minCount () {
      if (this.isSet && this.value) {
        return 1
      }
    }
  },
  'interests.$': { type: String, optional: true },
});

jankapunkt avatar Apr 25 '25 06:04 jankapunkt

Thank you @jankapunkt for your reply. Sadly this doesnt work. I tried it and I get the error message:

sanitizedError: errorClass [Error]: You must specify at least 1 values in peoples updateAsync [400]

juanpmd avatar Apr 25 '25 16:04 juanpmd