How can I tell the difference between an empty array and a null/undefined value when reading from a MongoDB with Mongoose? Mongoose reads both as an empty array, but in the database I have to work with the meanings of those is a different one.
Example:
var Mongoose = require('mongoose');
var MongoClient = require('mongodb').MongoClient;
Mongoose.connect('mongodb://localhost/test'); // mongoose
var Demo = Mongoose.model('demo', { names: [String] }, 'demo');
MongoClient.connect('mongodb://localhost/test', function (err, db) {
if (err) return console.error(err);
var collection = db.collection('demo');
// insert undefined array with MongoDB:
collection.insert({}, function(err, status) {
if(err) return console.error(err);
console.log('direct DB:', status.ops[0]);
// retrieve with Mongoose:
Demo.findOne({_id: status.insertedIds[0]}, function(err, doc) {
if(err) return console.error(err);
console.log('Mongoose:', doc);
});
});
});
When I run this code, it results in this output:
direct DB: { _id: 56b07f632390b15c15b4185d }
Mongoose: { names: [], _id: 56b07f632390b15c15b4185d }
So Mongoose sets an empty array where there shouldn't be one when reading from the database. I already tried setting name to undefined in post init hook, but it did not show any effect.
Any ideas how I could read this undef as an undef?