@@ -137,11 +137,9 @@ const methods = {
137137 idSchemaType = schemaType . schema . path ( '_id' ) ;
138138 } else if ( schemaType && schemaType . casterConstructor && schemaType . casterConstructor . schema ) {
139139 idSchemaType = schemaType . casterConstructor . schema . path ( '_id' ) ;
140- } else if ( this . length > 0 && this [ 0 ] != null && this [ 0 ] . $__schema ) {
141- idSchemaType = this [ 0 ] . $__schema . path ( '_id' ) ;
142140 }
143141
144- let castedId = id ;
142+ let castedId = null ;
145143 if ( idSchemaType ) {
146144 try {
147145 castedId = idSchemaType . cast ( id ) ;
@@ -150,7 +148,6 @@ const methods = {
150148 }
151149 }
152150
153- let sid ;
154151 let _id ;
155152
156153 for ( const val of this ) {
@@ -160,24 +157,20 @@ const methods = {
160157
161158 _id = val . get ( '_id' ) ;
162159
163- if ( _id === null || typeof _id === 'undefined' ) {
160+ if ( _id == null ) {
164161 continue ;
165162 } else if ( _id instanceof Document ) {
166- sid || ( sid = String ( id ) ) ;
167- if ( sid == _id . _id ) {
163+ _id = _id . get ( '_id' ) ;
164+ if ( castedId != null && utils . deepEqual ( castedId , _id ) ) {
168165 return val ;
169- }
170- } else if ( castedId != null && ! isBsonType ( castedId , 'ObjectId' ) && ! isBsonType ( _id , 'ObjectId' ) ) {
171- if ( castedId == _id || utils . deepEqual ( castedId , _id ) ) {
172- return val ;
173- }
174- } else if ( castedId != null && isBsonType ( castedId , 'ObjectId' ) && isBsonType ( _id , 'ObjectId' ) ) {
175- if ( castedId . toString ( ) === _id . toString ( ) ) {
166+ } else if ( castedId == null && ( id == _id || utils . deepEqual ( id , _id ) ) ) {
167+ // Backwards compat: compare user-specified param to _id using loose equality
176168 return val ;
177169 }
178- } else if ( castedId != null && castedId == _id ) {
170+ } else if ( castedId != null && utils . deepEqual ( castedId , _id ) ) {
179171 return val ;
180- } else if ( castedId == null && ( id == _id || utils . deepEqual ( id , _id ) ) ) {
172+ } else if ( castedId == null && ( _id == id || utils . deepEqual ( id , _id ) ) ) {
173+ // Backwards compat: compare user-specified param to _id using loose equality
181174 return val ;
182175 }
183176 }
0 commit comments