Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Automattic/mongoose
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 2, 2018
2 parents 551a75b + 1ca3514 commit d245847
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/aggregate.js
Expand Up @@ -13,7 +13,7 @@ const readConcern = Query.prototype.readConcern;

/**
* Aggregate constructor used for building aggregation pipelines. Do not
* instantiate this class directly, use [Model.aggregate()](/docs/api.html#aggregate_aggregate) instead.
* instantiate this class directly, use [Model.aggregate()](/docs/api.html#model_Model.aggregate) instead.
*
* ####Example:
*
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/query/castUpdate.js
Expand Up @@ -411,7 +411,7 @@ function castUpdateVal(schema, val, op, $conditional, context, path) {
return schema.castForQueryWrapper({
val: val,
context: context,
$skipQueryCastForUpdate: val != null && schema.$isMongooseArray
$skipQueryCastForUpdate: val != null && schema.$isMongooseArray && schema.$parentSchema
});
}

Expand Down
8 changes: 6 additions & 2 deletions lib/schema.js
Expand Up @@ -853,8 +853,12 @@ Schema.prototype.hasMixedParent = function(path) {
* @api private
*/
Schema.prototype.setupTimestamp = function(timestamps) {
const childHasTimestamp = this.childSchemas.
find(s => s.schema.options.timestamps != null);
const childHasTimestamp = this.childSchemas.find(withTimestamp);

function withTimestamp(s) {
const ts = s.schema.options.timestamps;
return ts == true || ts == null;
}

if (!timestamps && !childHasTimestamp) {
return;
Expand Down
42 changes: 42 additions & 0 deletions test/model.update.test.js
Expand Up @@ -2536,6 +2536,48 @@ describe('model: update:', function() {
catch(done);
});

it('doesn\'t skip casting the query on nested arrays (gh-7098)', function() {
const nestedSchema = new Schema({
xyz: [[Number]]
});
const schema = new Schema({
xyz: [[{ type: Number }]],
nested: nestedSchema
});

const Model = db.model('gh-7098', schema);

const test = new Model({
xyz: [
[0, 1],
[2, 3],
[4, 5]
],
nested: {
xyz: [
[0, 1],
[2, 3],
[4, 5]
],
}
});

const cond = { _id: test._id };
const update = { $set: { 'xyz.1.0': '200', 'nested.xyz.1.0': '200' } };
const opts = { new: true };

return co(function*() {
let inserted = yield test.save();
inserted = inserted.toObject();
assert.deepStrictEqual(inserted.xyz, [[0, 1], [2, 3], [4, 5]]);
assert.deepStrictEqual(inserted.nested.xyz, [[0, 1], [2, 3], [4, 5]]);
let updated = yield Model.findOneAndUpdate(cond, update, opts);
updated = updated.toObject();
assert.deepStrictEqual(updated.xyz, [[0, 1], [200, 3], [4, 5]]);
assert.deepStrictEqual(updated.nested.xyz, [[0, 1], [200, 3], [4, 5]]);
});
});

it('defaults with overwrite and no update validators (gh-5384)', function(done) {
const testSchema = new mongoose.Schema({
name: String,
Expand Down
18 changes: 18 additions & 0 deletions test/timestamps.test.js
Expand Up @@ -4,6 +4,7 @@ const assert = require('assert');
const start = require('./common');

const mongoose = start.mongoose;
const Schema = mongoose.Schema;

describe('timestamps', function() {
let db;
Expand Down Expand Up @@ -162,4 +163,21 @@ describe('timestamps', function() {
});
});
});

it('no timestamps added when parent/child timestamps explicitly false (gh-7202)', function(done) {
const subSchema = new Schema({}, { timestamps: false });
const schema = new Schema({ sub: subSchema }, { timestamps: false });

const Test = db.model('gh7202', schema);
const test = new Test({ sub: {} });

test.save((err, saved) => {
assert.ifError(err);
assert.strictEqual(saved.createdAt, undefined);
assert.strictEqual(saved.updatedAt, undefined);
assert.strictEqual(saved.sub.createdAt, undefined);
assert.strictEqual(saved.sub.updatedAt, undefined);
done();
});
});
});

0 comments on commit d245847

Please sign in to comment.