0

I'm building my own queries with the npm mongo-query-generator and I have quotes within the conditions. Is there any way to remove those quotes so I can apply regex properly?

What I currently have:

db.getCollection('products').find(
{"$and": 
    [{"$or": [
            {"category": "/cteos/i"},
            {"category": "/especiales/i"} ]}
    ,{"category": "/huevos/i"}
]})

What I want:

db.getCollection('products').find(
{"$and": 
    [{"$or": [
            {"category": /cteos/i},
            {"category": /especiales/i} ]}
    ,{"category": /huevos/i}
]})
Rober
  • 726
  • 8
  • 27
  • Are you sure you need to remove the quotes? There is [another way](http://stackoverflow.com/a/494046/6083675) to use regex in JS. – Laurel Aug 14 '16 at 16:27
  • I'm currently using the Regex approach but I can keep the quotes by using the mongo $regex expression – Rober Aug 14 '16 at 19:39

1 Answers1

1

This is the way I found to keep the quotes:

db.getCollection('products').find({
  "$and": [
    {
      "$or": [
        {
          "category": {
            "$regex": ".*cocinar.*",
            "$options": "i"
          }
        },
        {
          "category": {
            "$regex": ".*especiales.*",
            "$options": "i"
          }
        }
      ]
    },
    {
      "category": {
        "$regex": ".*harina.*",
        "$options": "i"
      }
    }
  ]

})
Rober
  • 726
  • 8
  • 27