@@ -28,6 +28,7 @@ import { WrongMethodException } from '#src/exceptions/WrongMethodException'
2828import { MONGO_OPERATIONS_DICTIONARY } from '#src/constants/MongoOperationsDictionary'
2929import { NotConnectedDatabaseException } from '#src/exceptions/NotConnectedDatabaseException'
3030import { NotImplementedMethodException } from '#src/exceptions/NotImplementedMethodException'
31+ import { ObjectId } from '#src/helpers/ObjectId'
3132
3233export class MongoDriver extends Driver < Connection , Collection > {
3334 public primaryKey = '_id'
@@ -1496,11 +1497,51 @@ export class MongoDriver extends Driver<Connection, Collection> {
14961497 const where : any = { }
14971498
14981499 if ( ! Is . Empty ( this . _where ) ) {
1499- where . $and = Json . copy ( this . _where )
1500+ where . $and = Json . copy ( this . _where ) . map ( condition => {
1501+ const keysToSwap = Object . keys ( condition ) . filter ( key => {
1502+ const value = condition [ key ]
1503+
1504+ if ( ObjectId . isValid ( value ) ) {
1505+ return true
1506+ }
1507+
1508+ return false
1509+ } )
1510+
1511+ keysToSwap . forEach ( key => {
1512+ const objectId = condition [ key ]
1513+
1514+ condition [ key ] = {
1515+ $or : [ { [ key ] : objectId } , { [ key ] : new ObjectId ( objectId ) } ]
1516+ }
1517+ } )
1518+
1519+ return condition
1520+ } )
15001521 }
15011522
15021523 if ( ! Is . Empty ( this . _orWhere ) ) {
1503- where . $or = Json . copy ( this . _orWhere )
1524+ where . $or = Json . copy ( this . _orWhere ) . map ( condition => {
1525+ const keysToSwap = Object . keys ( condition ) . filter ( key => {
1526+ const value = condition [ key ]
1527+
1528+ if ( ObjectId . isValid ( value ) ) {
1529+ return true
1530+ }
1531+
1532+ return false
1533+ } )
1534+
1535+ keysToSwap . forEach ( key => {
1536+ const objectId = condition [ key ]
1537+
1538+ condition [ key ] = {
1539+ $or : [ { [ key ] : objectId } , { [ key ] : new ObjectId ( objectId ) } ]
1540+ }
1541+ } )
1542+
1543+ return condition
1544+ } )
15041545 }
15051546
15061547 if ( options . clearWhere ) {
0 commit comments