Skip to content

Commit 291d972

Browse files
authored
Support ObjectID in findByFields() (#75)
The `findByFields()` method could not find documents by a given ObjectID because the `stringToId()` function was only being run when querying for a field named `_id`. Since any MongoDB field could in theory contain an ObjectID, this broke queries looking for ObjectIDs in arbitrary fields. To resolve this, remove the condition testing for the field name of `_id` and run `stringToId()` on all values. This will validate whether a given String value is actually in the ObjectID format before proceeding, so it seems safe to apply everywhere. Fixes #65
1 parent b40de70 commit 291d972

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/cache.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ export const createCachingMethods = ({ collection, model, cache }) => {
6969
let newVals = Array.isArray(fields[fieldName])
7070
? fields[fieldName]
7171
: [fields[fieldName]]
72-
if (docFieldName === '_id') newVals = newVals.map(stringToId)
72+
7373
filter[docFieldName].$in = [
7474
...filter[docFieldName].$in,
75-
...newVals.filter(val => !filter[docFieldName].$in.includes(val))
75+
...newVals
76+
.map(stringToId)
77+
.filter(val => !filter[docFieldName].$in.includes(val))
7678
]
7779
}
7880
if (existingFieldsFilter) return filterArray

0 commit comments

Comments
 (0)