@@ -3992,7 +3992,7 @@ describe('Query', function() {
3992
3992
} ) ;
3993
3993
} ) ;
3994
3994
3995
- it ( 'allows a transform option for lean on a query gh-10423' , async function ( ) {
3995
+ it ( 'allows a transform option for lean on a query ( gh-10423) ' , async function ( ) {
3996
3996
const arraySchema = new mongoose . Schema ( {
3997
3997
sub : String
3998
3998
} ) ;
@@ -4004,7 +4004,7 @@ describe('Query', function() {
4004
4004
foo : [ arraySchema ] ,
4005
4005
otherName : subDoc
4006
4006
} ) ;
4007
- const Test = db . model ( 'gh10423 ' , testSchema ) ;
4007
+ const Test = db . model ( 'Test ' , testSchema ) ;
4008
4008
await Test . create ( { name : 'foo' , foo : [ { sub : 'Test' } , { sub : 'Testerson' } ] , otherName : { nickName : 'Bar' } } ) ;
4009
4009
4010
4010
const result = await Test . find ( ) . lean ( {
@@ -4030,6 +4030,40 @@ describe('Query', function() {
4030
4030
assert . strictEqual ( single . foo [ 0 ] . _id , undefined ) ;
4031
4031
} ) ;
4032
4032
4033
+ it ( 'handles a lean transform that deletes _id with populate (gh-12143) (gh-10423)' , async function ( ) {
4034
+ const testSchema = Schema ( {
4035
+ name : String ,
4036
+ user : {
4037
+ type : mongoose . Types . ObjectId ,
4038
+ ref : 'User'
4039
+ }
4040
+ } ) ;
4041
+
4042
+ const userSchema = Schema ( {
4043
+ name : String
4044
+ } ) ;
4045
+
4046
+ const Test = db . model ( 'Test' , testSchema ) ;
4047
+ const User = db . model ( 'User' , userSchema ) ;
4048
+
4049
+ const user = await User . create ( { name : 'John Smith' } ) ;
4050
+ let test = await Test . create ( { name : 'test' , user } ) ;
4051
+
4052
+ test = await Test . findById ( test ) . populate ( 'user' ) . lean ( {
4053
+ transform : ( doc ) => {
4054
+ delete doc . _id ;
4055
+ delete doc . __v ;
4056
+ return doc ;
4057
+ }
4058
+ } ) ;
4059
+
4060
+ assert . ok ( test ) ;
4061
+ assert . deepStrictEqual ( test , {
4062
+ name : 'test' ,
4063
+ user : { name : 'John Smith' }
4064
+ } ) ;
4065
+ } ) ;
4066
+
4033
4067
it ( 'skips applying default projections over slice projections (gh-11940)' , async function ( ) {
4034
4068
const commentSchema = new mongoose . Schema ( {
4035
4069
comment : String
0 commit comments