最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make Mongoose not insert empty array or object fields into a document - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked May 12, 2017 at 9:51 DZNDZN 1,5843 gold badges24 silver badges49 bronze badges 1
  • Can you try assigning field3 : undefined in data variable while saving and check? – Mihir Bhende Commented May 12, 2017 at 10:12
Add a ment  | 

4 Answers 4

Reset to default 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 ment for more explanation.

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).

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 ing from mongoose?)

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 },
});
发布评论

评论列表(0)

  1. 暂无评论