@@ -3,10 +3,6 @@ import { ReadPreference, ReadPreferenceLike } from './read_preference';
3
3
import { deprecate } from 'util' ;
4
4
import {
5
5
normalizeHintField ,
6
- decorateCommand ,
7
- decorateWithCollation ,
8
- decorateWithReadConcern ,
9
- formattedOrderClause ,
10
6
checkCollectionName ,
11
7
deprecateOptions ,
12
8
MongoDBNamespace ,
@@ -91,8 +87,6 @@ import type { Topology } from './sdam/topology';
91
87
import type { Logger , LoggerOptions } from './logger' ;
92
88
import type { OperationParent } from './operations/command' ;
93
89
94
- const mergeKeys = [ 'ignoreUndefined' ] ;
95
-
96
90
/** @public */
97
91
export interface Collection {
98
92
/** @deprecated Use {@link Collection.dropIndexes#Class} instead */
@@ -138,6 +132,7 @@ export interface CollectionPrivate {
138
132
promoteLongs ?: boolean ;
139
133
promoteValues ?: boolean ;
140
134
promoteBuffers ?: boolean ;
135
+ ignoreUndefined ?: boolean ;
141
136
collectionHint ?: Hint ;
142
137
readConcern ?: ReadConcern ;
143
138
writeConcern ?: WriteConcern ;
@@ -212,7 +207,11 @@ export class Collection implements OperationParent {
212
207
promoteBuffers :
213
208
options == null || options . promoteBuffers == null
214
209
? db . s . options ?. promoteBuffers
215
- : options . promoteBuffers
210
+ : options . promoteBuffers ,
211
+ ignoreUndefined :
212
+ options == null || options . ignoreUndefined == null
213
+ ? db . s . options ?. ignoreUndefined
214
+ : options . ignoreUndefined
216
215
} ;
217
216
}
218
217
@@ -680,155 +679,24 @@ export class Collection implements OperationParent {
680
679
/**
681
680
* Creates a cursor for a query that can be used to iterate over results from MongoDB
682
681
*
683
- * @param query - The cursor query object.
684
- * @param options - Optional settings for the command
682
+ * @param filter - The query predicate. If unspecified, then all documents in the collection will match the predicate
685
683
*/
686
684
find ( ) : Cursor ;
687
- find ( query : Document ) : Cursor ;
688
- find ( query : Document , options : FindOptions ) : Cursor ;
689
- find ( query ?: Document , options ?: FindOptions ) : Cursor {
685
+ find ( filter : Document ) : Cursor ;
686
+ find ( filter : Document , options : FindOptions ) : Cursor ;
687
+ find ( filter ?: Document , options ?: FindOptions ) : Cursor {
690
688
if ( arguments . length > 2 ) {
691
689
throw new TypeError ( 'Third parameter to `collection.find()` must be undefined' ) ;
692
690
}
693
- if ( typeof query === 'function' ) {
694
- throw new TypeError ( '`query` parameter must not be function' ) ;
695
- }
696
691
if ( typeof options === 'function' ) {
697
692
throw new TypeError ( '`options` parameter must not be function' ) ;
698
693
}
699
694
700
- let selector =
701
- query !== null && typeof query === 'object' && Array . isArray ( query ) === false ? query : { } ;
702
-
703
- // Validate correctness off the selector
704
- const object = selector ;
705
- if ( Buffer . isBuffer ( object ) ) {
706
- const object_size = object [ 0 ] | ( object [ 1 ] << 8 ) | ( object [ 2 ] << 16 ) | ( object [ 3 ] << 24 ) ;
707
- if ( object_size !== object . length ) {
708
- const error = new Error (
709
- 'query selector raw message size does not match message header size [' +
710
- object . length +
711
- '] != [' +
712
- object_size +
713
- ']'
714
- ) ;
715
- error . name = 'MongoError' ;
716
- throw error ;
717
- }
718
- }
719
-
720
- // Check special case where we are using an objectId
721
- if ( selector != null && selector . _bsontype === 'ObjectID' ) {
722
- selector = { _id : selector } ;
723
- }
724
-
725
- if ( ! options ) {
726
- options = { } ;
727
- }
728
-
729
- let projection = options . projection || options . fields ;
730
-
731
- if ( projection && ! Buffer . isBuffer ( projection ) && Array . isArray ( projection ) ) {
732
- projection = projection . length
733
- ? projection . reduce ( ( result , field ) => {
734
- result [ field ] = 1 ;
735
- return result ;
736
- } , { } )
737
- : { _id : 1 } ;
738
- }
739
-
740
- // Make a shallow copy of options
741
- const newOptions : Document = Object . assign ( { } , options ) ;
742
-
743
- // Make a shallow copy of the collection options
744
- for ( const key in this . s . options ) {
745
- if ( mergeKeys . indexOf ( key ) !== - 1 ) {
746
- newOptions [ key ] = this . s . options [ key ] ;
747
- }
748
- }
749
-
750
- // Unpack options
751
- newOptions . skip = options . skip ? options . skip : 0 ;
752
- newOptions . limit = options . limit ? options . limit : 0 ;
753
- newOptions . raw = typeof options . raw === 'boolean' ? options . raw : this . s . raw ;
754
- newOptions . hint =
755
- options . hint != null ? normalizeHintField ( options . hint ) : this . s . collectionHint ;
756
- newOptions . timeout = typeof options . timeout === 'undefined' ? undefined : options . timeout ;
757
- // // If we have overridden slaveOk otherwise use the default db setting
758
- newOptions . slaveOk = options . slaveOk != null ? options . slaveOk : this . s . db . slaveOk ;
759
-
760
- // Add read preference if needed
761
- newOptions . readPreference = ReadPreference . resolve ( this , newOptions ) ;
762
-
763
- // Set slave ok to true if read preference different from primary
764
- if (
765
- newOptions . readPreference != null &&
766
- ( newOptions . readPreference !== 'primary' || newOptions . readPreference . mode !== 'primary' )
767
- ) {
768
- newOptions . slaveOk = true ;
769
- }
770
-
771
- // Ensure the query is an object
772
- if ( selector != null && typeof selector !== 'object' ) {
773
- throw new MongoError ( 'query selector must be an object' ) ;
774
- }
775
-
776
- // Build the find command
777
- const findCommand : Document = {
778
- find : this . s . namespace . toString ( ) ,
779
- limit : newOptions . limit ,
780
- skip : newOptions . skip ,
781
- query : selector
782
- } ;
783
-
784
- if ( typeof options . allowDiskUse === 'boolean' ) {
785
- findCommand . allowDiskUse = options . allowDiskUse ;
786
- }
787
-
788
- // Ensure we use the right await data option
789
- if ( typeof newOptions . awaitdata === 'boolean' ) {
790
- newOptions . awaitData = newOptions . awaitdata ;
791
- }
792
-
793
- // Translate to new command option noCursorTimeout
794
- if ( typeof newOptions . timeout === 'boolean' ) newOptions . noCursorTimeout = newOptions . timeout ;
795
-
796
- decorateCommand ( findCommand , newOptions , [ 'session' , 'collation' ] ) ;
797
-
798
- if ( projection ) findCommand . fields = projection ;
799
-
800
- // Add db object to the new options
801
- newOptions . db = this . s . db ;
802
-
803
- // Set raw if available at collection level
804
- if ( newOptions . raw == null && typeof this . s . raw === 'boolean' ) newOptions . raw = this . s . raw ;
805
- // Set promoteLongs if available at collection level
806
- if ( newOptions . promoteLongs == null && typeof this . s . promoteLongs === 'boolean' )
807
- newOptions . promoteLongs = this . s . promoteLongs ;
808
- if ( newOptions . promoteValues == null && typeof this . s . promoteValues === 'boolean' )
809
- newOptions . promoteValues = this . s . promoteValues ;
810
- if ( newOptions . promoteBuffers == null && typeof this . s . promoteBuffers === 'boolean' )
811
- newOptions . promoteBuffers = this . s . promoteBuffers ;
812
-
813
- // Sort options
814
- if ( findCommand . sort ) {
815
- findCommand . sort = formattedOrderClause ( findCommand . sort ) ;
816
- }
817
-
818
- // Set the readConcern
819
- decorateWithReadConcern ( findCommand , this , options ) ;
820
-
821
- // Decorate find command with collation options
822
-
823
- decorateWithCollation ( findCommand , this , options ) ;
824
-
825
- const cursor = new Cursor (
695
+ return new Cursor (
826
696
this . s . topology ,
827
- new FindOperation ( this , this . s . namespace , findCommand , newOptions ) ,
828
- newOptions
697
+ new FindOperation ( this , this . s . namespace , filter , options ) ,
698
+ options
829
699
) ;
830
-
831
- return cursor ;
832
700
}
833
701
834
702
/**
0 commit comments