File tree 1 file changed +14
-10
lines changed
1 file changed +14
-10
lines changed Original file line number Diff line number Diff line change @@ -3868,17 +3868,21 @@ Document.prototype.$toObject = function(options, json) {
3868
3868
if ( hasOnlyPrimitiveValues ) {
3869
3869
// Fast path: if we don't have any nested objects or arrays, we only need a
3870
3870
// shallow clone.
3871
- ret = this . _doc
3872
- ? options . minimize ? ( minimize ( { ...this . _doc } ) || { } ) : { ...this . _doc }
3873
- : { } ;
3874
- if ( ! options . minimize || options . flattenObjectIds ) {
3875
- const keys = Object . keys ( ret ) ;
3876
- for ( const key of keys ) {
3877
- // If `minimize` is false, still need to remove undefined keys
3878
- if ( ! options . minimize && ret [ key ] === undefined ) {
3871
+ if ( this . _doc == null ) {
3872
+ ret = { } ;
3873
+ }
3874
+ ret = { } ;
3875
+ if ( this . _doc != null ) {
3876
+ for ( const key of Object . keys ( this . _doc ) ) {
3877
+ const value = this . _doc [ key ] ;
3878
+ if ( value instanceof Date ) {
3879
+ ret [ key ] = new Date ( value ) ;
3880
+ } else if ( value === undefined ) {
3879
3881
delete ret [ key ] ;
3880
- } else if ( options . flattenObjectIds && isBsonType ( ret [ key ] , 'ObjectId' ) ) {
3881
- ret [ key ] = ret [ key ] . toJSON ( ) ;
3882
+ } else if ( options . flattenObjectIds && isBsonType ( value , 'ObjectId' ) ) {
3883
+ ret [ key ] = value . toJSON ( ) ;
3884
+ } else {
3885
+ ret [ key ] = value ;
3882
3886
}
3883
3887
}
3884
3888
}
You can’t perform that action at this time.
0 commit comments