Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:05:04+00:00 2026-05-29T21:05:04+00:00

I am using Node.js and Mongoose to store some data. After update, I have

  • 0

I am using Node.js and Mongoose to store some data. After update, I have the following structure:

 { created: Mon, 30 Jan 2012 19:25:57 GMT,
  _id: 4f21a6028132fba40f0000b7,
  features:
   { imdb_id: 'tt0822975',
     released: '2007-03-24',
     tvdb_id: 103191,
     type: 'series',
     names: [ 'DinoSapien' ],
     pictures: [],
     cast: [],
     genres: [ 'Action and Adventure', 'Children' ] },
  type: 1 }

I need to remove e.g. cast and pictures field on this document. However, I have applied a solution to remove empty arrays from the database but it does not work:

instance = (an instance from calling findOne on my model)
cast = (an array)
if ( cast && cast.length > 0){                          
     instance.features.cast = cast;                     
} else {
     delete instance.features.cast;
}
console.log(cast); // null
console.log(instance), // cast is not removed!

Is it possible to remove the empty arrays or unwanted values from model when saving to the db?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:05:08+00:00Added an answer on May 29, 2026 at 9:05 pm

    You can use a pre-save hook to check for those empty fields, and set them to undefined like this:

    PostSchema.pre('save', function (next) {
        if(this.pictures.length == 0){
            this.pictures = undefined;
        }
        if(this.cast.length == 0){ 
            this.cast = undefined;
        }
    
        next();
    });
    

    I tested this locally and it seems to do the job fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to gzip some data using Node.js... Specifically, I have data in 'buf'
I'm using Mongoose with Node.js and have the following code that will call the
I have the following controller/route definition in Node.js (using Express and Mongoose). What would
I have the following XPATH line: //det[@nItem=1]/prod/cProd That successfully selects the desired node using
I am using node.js with express. I read out data from MongoDB with Mongoose
I'm using Mongoosejs, with MongoDB and Node.js. I followed some online tutorials and created
I have a Node.js app using Express that stores data in a mongoDB (locally).
I have been using node and redis for some time. The problem I am
I'm attempting to check for the existence of a node using the following .NET
I have a mongoose model in my node.js application, representing invoices. I have figured

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.