Skip to content

Commit

Permalink
Merge branch 'master' into vkarpov15/gh-14394-2
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed May 30, 2024
2 parents 5d185b9 + a876c12 commit a40706b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
11 changes: 11 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3956,6 +3956,17 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op

_applyCustomWhere(document, where);

// If shard key is set, add shard keys to _filter_ condition to right shard is targeted
const shardKey = this.schema.options.shardKey;
if (shardKey) {
const paths = Object.keys(shardKey);
const len = paths.length;

for (let i = 0; i < len; ++i) {
where[paths[i]] = shardKey[paths[i]];
}
}

// Set the discriminator key, so bulk write casting knows which
// schema to use re: gh-13907
if (document[discriminatorKey] != null && !(discriminatorKey in where)) {
Expand Down
10 changes: 5 additions & 5 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6331,14 +6331,14 @@ describe('Model', function() {

const userSchema = new Schema({
name: { type: String }
});
}, { shardKey: { a: 1 } });

const User = db.model('User', userSchema);

const users = [
new User({ name: 'Hafez1_gh-9673-1' }),
new User({ name: 'Hafez2_gh-9673-1' }),
new User({ name: 'I am the third name' })
new User({ name: 'Hafez1_gh-9673-1', a: 1 }),
new User({ name: 'Hafez2_gh-9673-1', a: 2 }),
new User({ name: 'I am the third name', a: 3 })
];

await users[2].save();
Expand All @@ -6349,7 +6349,7 @@ describe('Model', function() {
const desiredWriteOperations = [
{ insertOne: { document: users[0] } },
{ insertOne: { document: users[1] } },
{ updateOne: { filter: { _id: users[2]._id }, update: { $set: { name: 'I am the updated third name' } } } }
{ updateOne: { filter: { _id: users[2]._id, a: 1 }, update: { $set: { name: 'I am the updated third name' } } } }
];

assert.deepEqual(
Expand Down
40 changes: 39 additions & 1 deletion test/types/subdocuments.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Schema, model, Model, Document, Types } from 'mongoose';
import {
Schema,
model,
Model,
Document,
Types,
HydratedArraySubdocument,
HydratedSingleSubdocument
} from 'mongoose';
import { expectAssignable } from 'tsd';

const childSchema: Schema = new Schema({ name: String });

Expand Down Expand Up @@ -108,3 +117,32 @@ function gh13040(): void {
product.ownerDocument();
});
}

function gh14601() {
interface ISub {
field1: string;
}
interface IMain {
f1: string;
f2: HydratedSingleSubdocument<ISub>;
f3: HydratedArraySubdocument<ISub>[];
}

const subSchema = new Schema({ field1: String }, { _id: false });

const mainSchema = new Schema({
f1: String,
f2: { type: subSchema },
f3: { type: [subSchema] }
});
const MainModel = model<IMain>('Main', mainSchema);

const item = new MainModel({
f1: 'test',
f2: { field1: 'test' },
f3: [{ field1: 'test' }]
});

const f2obj = item.f2.toObject();
expectAssignable<{ _id: Types.ObjectId, field1: string }>(f2obj);
}
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ declare module 'mongoose' {
>
>
>;
export type HydratedSingleSubdocument<DocType, TOverrides = {}> = Types.Subdocument<unknown> & Require_id<DocType> & TOverrides;
export type HydratedArraySubdocument<DocType, TOverrides = {}> = Types.ArraySubdocument<unknown> & Require_id<DocType> & TOverrides;
export type HydratedSingleSubdocument<DocType, TOverrides = {}> = Types.Subdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> & TOverrides;
export type HydratedArraySubdocument<DocType, TOverrides = {}> = Types.ArraySubdocument<unknown, Record<string, never>, DocType> & Require_id<DocType> & TOverrides;

export type HydratedDocumentFromSchema<TSchema extends Schema> = HydratedDocument<
InferSchemaType<TSchema>,
Expand Down
2 changes: 1 addition & 1 deletion types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ declare module 'mongoose' {
$parent(): Document;
}

class ArraySubdocument<IdType = any> extends Subdocument<IdType> {
class ArraySubdocument<IdType = any, TQueryHelpers = unknown, DocType = unknown> extends Subdocument<IdType, TQueryHelpers, DocType> {
/** Returns this sub-documents parent array. */
parentArray(): Types.DocumentArray<unknown>;
}
Expand Down

0 comments on commit a40706b

Please sign in to comment.