New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle discriminators with insertMany()
#7818
Comments
I can't reproduce this on 5.5.9. repro script (can't reproduce) /**
* When you use `Model.create()`, mongoose will pull the correct type from
* the discriminator key for you.
*/
it('Using discriminators with `Model.create()` or `Model.insertMany()`', function(done) {
var Schema = mongoose.Schema;
var shapeSchema = new Schema({
name: String
}, { discriminatorKey: 'kind' });
var squareSchema = new Schema({ side: Number });
var validateCount = 0;
squareSchema.pre('validate', function() {
validateCount++;
});
var Shape = db.model('Shape', shapeSchema);
var Circle = Shape.discriminator('Circle',
new Schema({ radius: Number }));
var Square = Shape.discriminator('Square', squareSchema);
var shapeObjects = [
{ name: 'Test' },
{ kind: 'Circle', radius: 5 },
{ kind: 'Square', side: 10 }
];
Shape.create(shapeObjects).then(function(shapes) { // test model.create
assert.ok(shapes[0] instanceof Shape);
assert.ok(shapes[1] instanceof Circle);
assert.equal(shapes[1].radius, 5);
assert.ok(shapes[2] instanceof Square);
assert.equal(shapes[2].side, 10);
return Shape.insertMany(shapeObjects); // test model.insertMany
}).then(function(shapes) {
assert.ok(shapes[0] instanceof Shape);
assert.ok(shapes[1] instanceof Circle);
assert.equal(shapes[1].radius, 5);
assert.ok(shapes[2] instanceof Square);
assert.equal(shapes[2].side, 10);
assert.equal(validateCount, 2);
done();
}).
catch(done);
}); Data in DB $ npm test -- -g "Using discriminators with"
> mongoose@5.5.9 test /home/fonger/mongoose
> mocha --exit test/*.test.js test/**/*.test.js "-g" "Using discriminators with"
You're not testing shards!
Please set the MONGOOSE_SHARD_TEST_URI env variable.
e.g: `mongodb://localhost:27017/database
Sharding must already be enabled on your database
․
1 passing (130ms)
$ mongo
> use mongoose_test
switched to db mongoose_test
> db.shapes.find({}).pretty()
{ "_id" : ObjectId("5ce000d3a6a1014a1cc9fc46"), "name" : "Test", "__v" : 0 }
{
"_id" : ObjectId("5ce000d3a6a1014a1cc9fc47"),
"__t" : "Circle",
"radius" : 5,
"__v" : 0
}
{
"_id" : ObjectId("5ce000d3a6a1014a1cc9fc48"),
"__t" : "Square",
"side" : 10,
"__v" : 0
}
{ "_id" : ObjectId("5ce000d3a6a1014a1cc9fc49"), "name" : "Test", "__v" : 0 }
{
"_id" : ObjectId("5ce000d3a6a1014a1cc9fc4a"),
"__t" : "Circle",
"radius" : 5,
"__v" : 0
}
{
"_id" : ObjectId("5ce000d3a6a1014a1cc9fc4b"),
"__t" : "Square",
"side" : 10,
"__v" : 0
} Unlike Model.create() new model based on discriminator keyLines 2874 to 2882 in 5011b56
Lines 2894 to 2897 in 5011b56
Model.insertMany() new model based on
|
arr.forEach(function(doc) { | |
toExecute.push(function(callback) { | |
if (!(doc instanceof _this)) { | |
doc = new _this(doc); | |
} |
Yes, it seems that there was a change between version var ShapeSchema = new db.Schema({
type: {type: String},
name: String,
}, {collection: 'shapes', discriminatorKey: 'type'});
var ShapeModel = db.model('ShapeModel', ShapeSchema);
var SquareSchema = new db.Schema({
radius: Number
});
var CircleModel = ShapeModel.discriminator('Circle', SquareSchema);
var dalDoc = new ShapeModel({type: 'Circle', radius: 12});
dalDoc.save().then(res => {
console.log('Inserted --->> ', res.toJSON());
});
//// Output on 5.3.15
Inserted --->> { _id: 5ce10ca93ca3ff1964d15ce1, type: 'Circle', __v: 0 }
//// Output on 5.5.9
Inserted --->> { _id: 5ce10cdc6d05443ba4107f43,
type: 'Circle',
radius: 12,
__v: 0
} So looks good :) |
Re: #6087 (comment), there may be an issue with
BaseModel.insertMany()
not respecting discriminator keys.The text was updated successfully, but these errors were encountered: