LearnBoost/mongoose — Issue #736: Inserting subdocuments in Array Silent Failure with { strict : true }
Author: glesperance
Created: 2012-02-14 17:35:40+00:00
State: open

It seems that the current version of mongoose silently fails to $addToSetEach subdocs / objects in an array of a document if the subdocument weren't defined in the schema.

If you run the following test code snippet ;

var mongoose = require('mongoose');

var MONGO_SERVER = 'localhost'
, MONGO_DB_NAME = 'test'
;

var TestDocSchema = new mongoose.Schema({}, { strict : true });

mongoose.connect('mongodb://' + MONGO_SERVER + '/' + MONGO_DB_NAME);
mongoose.connection.on('error', function(err) {
console.log(err);
});

var TestDocument = mongoose.model('TestDocument', TestDocSchema);

var updateQuery = { $addToSet: { unmappedArray: {$each: [1,2,3,4] } } };

var testDoc = new TestDocument();

testDoc.save(function(err) {
if (err) { console.log('error creating new test doc:', err); }
else { TestDocument.update({}, updateQuery, function() { console.log(arguments); }); }
});

and then do the following in the mongo console :

db.testdocuments.find()

You have the following result :
{ "_id" : ObjectId("4f3a99e8547563172a000001"), "unmappedArray" : [ { } ] }

IMHO, mongoose shouldn't fail silently as it complicates the debugging process and so really defeats teh purpose of using `{ strict : true }`.