mongoose 8.9.4 → 8.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -191,15 +191,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
191
191
  if (hasMatchFunction) {
192
192
  match = match.call(doc, doc);
193
193
  }
194
- if (Array.isArray(match)) {
195
- for (const item of match) {
196
- if (item != null && item.$where) {
197
- throw new MongooseError('Cannot use $where filter with populate() match');
198
- }
199
- }
200
- } else if (match != null && match.$where != null) {
201
- throw new MongooseError('Cannot use $where filter with populate() match');
202
- }
194
+ throwOn$where(match);
203
195
  data.match = match;
204
196
  data.hasMatchFunction = hasMatchFunction;
205
197
  data.isRefPath = isRefPath;
@@ -463,15 +455,7 @@ function _virtualPopulate(model, docs, options, _virtualRes) {
463
455
  data.match = match;
464
456
  data.hasMatchFunction = hasMatchFunction;
465
457
 
466
- if (Array.isArray(match)) {
467
- for (const item of match) {
468
- if (item != null && item.$where) {
469
- throw new MongooseError('Cannot use $where filter with populate() match');
470
- }
471
- }
472
- } else if (match != null && match.$where != null) {
473
- throw new MongooseError('Cannot use $where filter with populate() match');
474
- }
458
+ throwOn$where(match);
475
459
 
476
460
  // Get local fields
477
461
  const ret = _getLocalFieldValues(doc, localField, model, options, virtual);
@@ -759,3 +743,24 @@ function _findRefPathForDiscriminators(doc, modelSchema, data, options, normaliz
759
743
 
760
744
  return modelNames;
761
745
  }
746
+
747
+ /**
748
+ * Throw an error if there are any $where keys
749
+ */
750
+
751
+ function throwOn$where(match) {
752
+ if (match == null) {
753
+ return;
754
+ }
755
+ if (typeof match !== 'object') {
756
+ return;
757
+ }
758
+ for (const key of Object.keys(match)) {
759
+ if (key === '$where') {
760
+ throw new MongooseError('Cannot use $where filter with populate() match');
761
+ }
762
+ if (match[key] != null && typeof match[key] === 'object') {
763
+ throwOn$where(match[key]);
764
+ }
765
+ }
766
+ }
@@ -209,7 +209,7 @@ SchemaBigInt.prototype.castForQuery = function($conditional, val, context) {
209
209
  return handler.call(this, val);
210
210
  }
211
211
 
212
- return this.applySetters(null, val, context);
212
+ return this.applySetters(val, context);
213
213
  }
214
214
 
215
215
  try {
@@ -253,7 +253,7 @@ SchemaBoolean.prototype.castForQuery = function($conditional, val, context) {
253
253
  return handler.call(this, val);
254
254
  }
255
255
 
256
- return this.applySetters(null, val, context);
256
+ return this.applySetters(val, context);
257
257
  }
258
258
 
259
259
  try {
@@ -7,6 +7,7 @@
7
7
  const CastError = require('../error/cast');
8
8
  const SchemaType = require('../schemaType');
9
9
  const castInt32 = require('../cast/int32');
10
+ const handleBitwiseOperator = require('./operators/bitwise');
10
11
 
11
12
  /**
12
13
  * Int32 SchemaType constructor.
@@ -200,7 +201,11 @@ SchemaInt32.$conditionalHandlers = {
200
201
  $gt: handleSingle,
201
202
  $gte: handleSingle,
202
203
  $lt: handleSingle,
203
- $lte: handleSingle
204
+ $lte: handleSingle,
205
+ $bitsAllClear: handleBitwiseOperator,
206
+ $bitsAnyClear: handleBitwiseOperator,
207
+ $bitsAllSet: handleBitwiseOperator,
208
+ $bitsAnySet: handleBitwiseOperator
204
209
  };
205
210
 
206
211
  /*!
@@ -228,7 +233,7 @@ SchemaInt32.prototype.castForQuery = function($conditional, val, context) {
228
233
  return handler.call(this, val);
229
234
  }
230
235
 
231
- return this.applySetters(null, val, context);
236
+ return this.applySetters(val, context);
232
237
  }
233
238
 
234
239
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.9.4",
4
+ "version": "8.9.5",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
package/valnotes.md ADDED
@@ -0,0 +1,85 @@
1
+ ## sift where
2
+
3
+ ```
4
+ it('match prevents using $where', async function() {
5
+ const ParentSchema = new Schema({
6
+ name: String,
7
+ child: {
8
+ type: mongoose.Schema.Types.ObjectId,
9
+ ref: 'Child'
10
+ },
11
+ children: [{
12
+ type: mongoose.Schema.Types.ObjectId,
13
+ ref: 'Child'
14
+ }]
15
+ });
16
+
17
+ const ChildSchema = new Schema({
18
+ name: String
19
+ });
20
+ ChildSchema.virtual('parent', {
21
+ ref: 'Parent',
22
+ localField: '_id',
23
+ foreignField: 'parent'
24
+ });
25
+
26
+ const Parent = db.model('Parent', ParentSchema);
27
+ const Child = db.model('Child', ChildSchema);
28
+
29
+ const child = await Child.create({ name: 'Luke' });
30
+ const parent = await Parent.create({ name: 'Anakin', child: child._id });
31
+
32
+ await assert.rejects(
33
+ () => Parent.findOne().populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
34
+ /Cannot use \$where filter with populate\(\) match/
35
+ );
36
+ await assert.rejects(
37
+ () => Parent.find().populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
38
+ /Cannot use \$where filter with populate\(\) match/
39
+ );
40
+ await assert.rejects(
41
+ () => parent.populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
42
+ /Cannot use \$where filter with populate\(\) match/
43
+ );
44
+ await assert.rejects(
45
+ () => Child.find().populate({ path: 'parent', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
46
+ /Cannot use \$where filter with populate\(\) match/
47
+ );
48
+ await assert.rejects(
49
+ () => Child.find().populate({ path: 'parent', match: () => ({ $or: [{ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }] }) }),
50
+ /Cannot use \$where filter with populate\(\) match/
51
+ );
52
+ await assert.rejects(
53
+ () => Child.find().populate({ path: 'parent', match: () => ({ $and: [{ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }] }) }),
54
+ /Cannot use \$where filter with populate\(\) match/
55
+ );
56
+
57
+ class MyClass {}
58
+ MyClass.prototype.$where = 'typeof console !== "undefined" ? doesNotExist("foo") : true;';
59
+ // OK because sift only looks through own properties
60
+ await Child.find().populate({ path: 'parent', match: () => new MyClass() });
61
+ });
62
+ ```
63
+
64
+ ```
65
+ /**
66
+ * Throw an error if there are any $where keys
67
+ */
68
+
69
+ function throwOn$where(match) {
70
+ if (match == null) {
71
+ return;
72
+ }
73
+ if (typeof match !== 'object') {
74
+ return;
75
+ }
76
+ for (const key of Object.keys(match)) {
77
+ if (key === '$where') {
78
+ throw new MongooseError('Cannot use $where filter with populate() match');
79
+ }
80
+ if (match[key] != null && typeof match[key] === 'object') {
81
+ throwOn$where(match[key]);
82
+ }
83
+ }
84
+ }
85
+ ```