0

I am trying to reproduce the REPLACE function in sql on mongodb.

My collection 'log' has this data in the 'text' field.

[01]ABC0007[0d0a]BB BABLOXC[0d0a]067989 PLPXBNS[0d0a02]BBR OIC002 L5U0/P AMD KAP 041800 T1200AND 2+00[0d0a0b03]

All I'm trying to do is remove the '[..]' using regex (javascript) and use contains('PLPXBNSBBR') like this so that the expression return true per the javadocs in mongo documentation.

This query below successfully works and returns the matching rows.

db.log.find({"$where":"return this.message.replace(new RegExp('0d0a02'),'').contains('PLPXBNS[]BBR') "});

However, I would need to remove the '[..]' and match like this PLPXBNSBBR.

These are the ones I tried unsuccessfully

db.log.find({"$where" : " return this.message.replace( new RegExp('\[.*?\]', 'g'), '' ).contains('PLPXBNSBBR') " });

db.log.find({"$where" : " return this.message.replace( new RegExp('/[(.*?)]', 'g'), '' ).contains('PLPXBNSBBR') " });

db.log.find({"$where" : " return this.message.replace( new RegExp('//[.*//]'), '' ).contains('PLPXBNSBBR') " });

db.log.find({"$where" : " return this.message.replace( new RegExp('[.*?]'), '' ).contains('PLPXBNSBBR') " });

From the earlier discussion it appears that if I can pass the pattern as /[[^]]+]/g, it should strip the [..] but it is not doing that and not returning the matching rows.

HVA
  • 43
  • 6

1 Answers1

0

Okay, I was able to use chaining replace successfully to get my desired results.

HVA
  • 43
  • 6