4

Let's say we have a Mongoose schema in our Node.js project:

let coolSchema = new mongoose.Schema({
  field_1 : Number,
  field_2 : String,
  field_3 : [ String ],
});

And let's we have an according object:

var data = {
  field_1 : 123,
  field_2 : 'blah',
  field_3 : ['aa', 'bb'],
};

Now to save this data into MongoDB we can use this code:

let Model = require('mongoose').model('CoolModel', coolSchema);
(new Model(data)).save();

Ok, while it's all cool.

But if data does not contain field_3 (array field, and the same will be for an object field) Mongoose will anyway add this field into the being created document with empty value.

Can we somehow tell Mongoose not to create this field if it's not contained in the data object?

1
  • Can you try assigning field3 : undefined in data variable while saving and check? Commented May 12, 2017 at 10:12

4 Answers 4

5

The accepted answer is good. But if you wouldn't want to use pre-hook, then you can add default: undefined to the array fields. For example:

var schema = new Schema({
  myArr: { type: [String], default: undefined }
});

Refer to this comment for more explanation.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer
3

you can do it easily skip the array field and array of object field.. This will let you skip saving empty array in new documents.but you have to use pre hook for this .

var brandSchema = new Schema({
    name : {type:String},
    email:String,
    check:[]
})

brandSchema.pre('save', function (next) {
  if (this.isNew && 0 === this.check.length) {
    this.check = undefined;                                                                                                                                   
  }
  next();
})

when new document is inserted in your schema you have to use this middlware.this works fine so try this. this is the response when we want to insert any document

"data": {
    "__v": 0,
    "name": "testing",
    "email": "[email protected]",
    "_id": "5915b018e292833edda8837f"
  }

so i have send only email and name but check(array) field is skipped(Not send any value).

Comments

1

Not particularly an answer to the question itself but some thought on the matter.

It's not clear exactly what you're trying to achieve here. You defined a schema that is supposed to contain a list of string. Mongoose correctly does so that the data saved in your schema is consistent with the definition of the schema.

In this case, the list is more of a structural part of the schema. If you have different behaviour, you'd have to handle special case in your code in case the list isn't present. Now, you can safely assume that you schema is always returning a list so fetching some data will always allow you to do:

coolData.field_3.forEach(function(x) {
   do_cool_things(x)
})

What you're asking is to make the schema allow inconsistent data being returned from mongodb... In other words, you'd have to do this in order to prevent accessing attributes on undefined:

if (coolData.field_3) {
   coolData.field_3.forEach(function(x) {
      do_cool_things(x)
   })
}

Also, I you were trying to optimize the size of you objects/database, you could fill a bug report so mongoose doesn't define empty values while saving the objects and autofill them with defaults when the field is missing from mongodb. (I could be wrong but did you actually check if the data in mongodb was containing empty values or you were just looking at data coming from mongoose?)

1 Comment

thanks for sharing your thoughts. I will think more about my data structure... (yes, I've checked - document in mongodb contains array with no items)
-4

It's because you're not marking the fields as required in your schema definition. Do this:

let coolSchema = new mongoose.Schema({
  field_1 : { type: Number, required: true },
  field_2 : { type: String, required: true },
  field_3 : { type: [ String ], required: true },
});

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.