Don't store empty strings

Hi everyone,

i would like to store a post as a document in mongodb. I’m using mongoose for modelling and the content is created by a user using a form. The content of the form is append to FormData and sending to server. This works so far. The only issue is, that empty fields, that are appended as empty strings in the req.body will be stored in the document. The minimalize-property of my dataschema is already set true …

const post = req.body;
await Post.create(post);

req.body looks like:
[Object: null prototype] {

  image: '',
  title: 'hi',
  subtitle: '',
  category: 'Jobs',
  tags: '',
  text: '',
  contactperson: '',
  contact: '',
  author: 'Felicia',
  expires: '2022-08-06'
}

Thanks so much for your help!

Hi @Felicia_Debye,

My interpretation is that you do not want to store fields that have empty string values in the database. Please correct me if I am wrong here.

In saying so, I have created a small test snippet which performs the above based off the Schema using customer setter. This is just one alternate method that may suit your use case. Please feel free to check any mongoose plugins if you wish to do more research.

Example test code using the req.body value you have provided:

import mongoose from 'mongoose';
const { Schema } = mongoose;

mongoose.connect('mongodb+srv://USER:PASS@testcluster.abcde.mongodb.net/mongoose?retryWrites=true&w=majority', () => {
    console.log("db connected successfully..")
})

const bodySchema = new Schema({
	image: { type: String, required: false, set: a => a === '' ? undefined : a},
	title: { type: String, required: false, set: b => b === '' ? undefined : b},
	subtitle: { type: String, required: false, set: c => c === '' ? undefined : c},
	category: { type: String, required: false, set: d => d === '' ? undefined : d},
	tags: { type: String, required: false, set: e => e === '' ? undefined : e},
	text: { type: String, required: false, set: f => f === '' ? undefined : f},
	contactperson: { type: String, required: false, set: g => g === '' ? undefined : g},
	contact: { type: String, required: false, set: h => h === '' ? undefined : h},
	author: { type: String, required: false, set: i => i === '' ? undefined : i},
	expires: { type: String, required: false, set: j => j === '' ? undefined : j}
},{ minimize: true })

const Body = mongoose.model('Body', bodySchema);

await Body.create({
  image: '',
  title: 'hi',
  subtitle: '',
  category: 'Jobs',
  tags: '',
  text: '',
  contactperson: '',
  contact: '',
  author: 'Felicia',
  expires: '2022-08-06'
})

console.log("doc inserted")

The resulting document that is inserted:

{
"_id":{"$oid":"62e89ea6eb8a35c11e33b220"},
"title":"hi",
"category":"Jobs",
"author":"Felicia",
"expires":"2022-08-06",
"__v":{"$numberInt":"0"}
}

(In Data Explorer):

With any of these snippets, it is highly recommended you test thoroughly in a test environment to see if it suits your use case(s) and meets all your requirements. I have only tested this on a singular document based off the req.body value you had provided.

Hope this helps.

Regards,
Jason

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.