简体   繁体   中英

Mongoose: Is field set in the database?

I have a Mongoose schema with a property that holds an array of strings. Something like:

var schema = mongoose.schema({
  myProp: [String]
})
mongoose.model('MyModel', schema);

Now, when I load a MyModel document and check its .myProp property, I get an empty array (thanks to Mongoose's casting, I think), even if the document in Mongo has no myProp field.

How can I distinguish from my javascript between the document in mongo having no myProp field, and having a myProp field whose value is an empty array, given that doc.myProp returns an empty array in both cases?

You could always use the lean() option when querying, which returns exactly what is in Mongo without creating an instance of the Mongoose model.

Model.find().lean().exec(function (err, docs) {
  // docs should be identical to your mongo collection
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM