@@ -5,7 +5,7 @@ var mongoose = require('mongoose'),
55
66/** 
77 * This code is taken from official mongoose repository 
8-  * https://github.com/Automattic/mongoose/blob/master/lib/query.js#L1996-L2018  
8+  * https://github.com/Automattic/mongoose/blob/master/lib/query.js#L3847-L3873  
99 */ 
1010/* istanbul ignore next */ 
1111function  parseUpdateArguments  ( conditions ,  doc ,  options ,  callback )  { 
@@ -78,10 +78,11 @@ function createSchemaObject (typeKey, typeValue, options) {
7878
7979module . exports  =  function  ( schema ,  options )  { 
8080 options  =  options  ||  { } ; 
81-  var  indexFields  =  parseIndexFields ( options ) 
81+  var  indexFields  =  parseIndexFields ( options ) ; 
8282
8383 var  typeKey  =  schema . options . typeKey ; 
84- 
84+  var  mongooseMajorVersion  =  + mongoose . version [ 0 ] ;  // 4, 5... 
85+  var  mainUpdateMethod  =  mongooseMajorVersion  <  5  ? 'update'  : 'updateMany' ; 
8586 schema . add ( {  deleted : createSchemaObject ( typeKey ,  Boolean ,  {  default : false ,  index : indexFields . deleted  } )  } ) ; 
8687
8788 if  ( options . deletedAt  ===  true )  { 
@@ -92,6 +93,11 @@ module.exports = function (schema, options) {
9293 schema . add ( {  deletedBy : createSchemaObject ( typeKey ,  options . deletedByType  ||  Schema . Types . ObjectId ,  {  index : indexFields . deletedBy  } )  } ) ; 
9394 } 
9495
96+  var  use$neOperator  =  true ; 
97+  if  ( options . use$neOperator  !==  undefined  &&  typeof  options . use$neOperator  ===  "boolean" )  { 
98+  use$neOperator  =  options . use$neOperator ; 
99+  } 
100+ 
95101 schema . pre ( 'save' ,  function  ( next )  { 
96102 if  ( ! this . deleted )  { 
97103 this . deleted  =  false ; 
@@ -101,7 +107,7 @@ module.exports = function (schema, options) {
101107
102108 if  ( options . overrideMethods )  { 
103109 var  overrideItems  =  options . overrideMethods ; 
104-  var  overridableMethods  =  [ 'count' ,  'countDocuments' ,  'find' ,  'findOne' ,  'findOneAndUpdate' ,  'update' ] ; 
110+  var  overridableMethods  =  [ 'count' ,  'countDocuments' ,  'find' ,  'findOne' ,  'findOneAndUpdate' ,  'update' ,   'updateMany' ] ; 
105111 var  finalList  =  [ ] ; 
106112
107113 if  ( ( typeof  overrideItems  ===  'string'  ||  overrideItems  instanceof  String )  &&  overrideItems  ===  'all' )  { 
@@ -126,15 +132,23 @@ module.exports = function (schema, options) {
126132
127133 // countDocuments do not exist in Mongoose v4 
128134 /* istanbul ignore next */ 
129-  if  ( method  ===  'countDocuments'  &&  typeof  Model . countDocuments  !==  'function' )  { 
135+  if  ( mongooseMajorVersion   <   5   &&   method  ===  'countDocuments'  &&  typeof  Model . countDocuments  !==  'function' )  { 
130136 modelMethodName  =  'count' ; 
131137 } 
132138
133139 schema . statics [ method ]  =  function  ( )  { 
134-  return  Model [ modelMethodName ] . apply ( this ,  arguments ) . where ( 'deleted' ) . ne ( true ) ; 
140+  if  ( use$neOperator )  { 
141+  return  Model [ modelMethodName ] . apply ( this ,  arguments ) . where ( 'deleted' ) . ne ( true ) ; 
142+  }  else  { 
143+  return  Model [ modelMethodName ] . apply ( this ,  arguments ) . where ( { deleted : false } ) ; 
144+  } 
135145 } ; 
136146 schema . statics [ method  +  'Deleted' ]  =  function  ( )  { 
137-  return  Model [ modelMethodName ] . apply ( this ,  arguments ) . where ( 'deleted' ) . ne ( false ) ; 
147+  if  ( use$neOperator )  { 
148+  return  Model [ modelMethodName ] . apply ( this ,  arguments ) . where ( 'deleted' ) . ne ( false ) ; 
149+  }  else  { 
150+  return  Model [ modelMethodName ] . apply ( this ,  arguments ) . where ( { deleted : true } ) ; 
151+  } 
138152 } ; 
139153 schema . statics [ method  +  'WithDeleted' ]  =  function  ( )  { 
140154 return  Model [ modelMethodName ] . apply ( this ,  arguments ) ; 
@@ -143,15 +157,23 @@ module.exports = function (schema, options) {
143157 schema . statics [ method ]  =  function  ( )  { 
144158 var  args  =  parseUpdateArguments . apply ( undefined ,  arguments ) ; 
145159
146-  args [ 0 ] . deleted  =  { '$ne' : true } ; 
160+  if  ( use$neOperator )  { 
161+  args [ 0 ] . deleted  =  { '$ne' : true } ; 
162+  }  else  { 
163+  args [ 0 ] . deleted  =  false ; 
164+  } 
147165
148166 return  Model [ method ] . apply ( this ,  args ) ; 
149167 } ; 
150168
151169 schema . statics [ method  +  'Deleted' ]  =  function  ( )  { 
152170 var  args  =  parseUpdateArguments . apply ( undefined ,  arguments ) ; 
153171
154-  args [ 0 ] . deleted  =  { '$ne' : false } ; 
172+  if  ( use$neOperator )  { 
173+  args [ 0 ] . deleted  =  { '$ne' : false } ; 
174+  }  else  { 
175+  args [ 0 ] . deleted  =  true ; 
176+  } 
155177
156178 return  Model [ method ] . apply ( this ,  args ) ; 
157179 } ; 
@@ -165,8 +187,8 @@ module.exports = function (schema, options) {
165187
166188 schema . methods . delete  =  function  ( deletedBy ,  cb )  { 
167189 if  ( typeof  deletedBy  ===  'function' )  { 
168-  cb  =  deletedBy 
169-  deletedBy  =  null 
190+  cb  =  deletedBy ; 
191+  deletedBy  =  null ; 
170192 } 
171193
172194 this . deleted  =  true ; 
@@ -212,7 +234,7 @@ module.exports = function (schema, options) {
212234 if  ( this . updateWithDeleted )  { 
213235 return  this . updateWithDeleted ( conditions ,  doc ,  {  multi : true  } ,  callback ) ; 
214236 }  else  { 
215-  return  this . update ( conditions ,  doc ,  {  multi : true  } ,  callback ) ; 
237+  return  this [ mainUpdateMethod ] ( conditions ,  doc ,  {  multi : true  } ,  callback ) ; 
216238 } 
217239 } ; 
218240
@@ -251,7 +273,7 @@ module.exports = function (schema, options) {
251273 if  ( this . updateWithDeleted )  { 
252274 return  this . updateWithDeleted ( conditions ,  doc ,  {  multi : true  } ,  callback ) ; 
253275 }  else  { 
254-  return  this . update ( conditions ,  doc ,  {  multi : true  } ,  callback ) ; 
276+  return  this [ mainUpdateMethod ] ( conditions ,  doc ,  {  multi : true  } ,  callback ) ; 
255277 } 
256278 } ; 
257279} ; 
0 commit comments