Rober
Rober

Reputation: 726

Mongo regex query remove quotes

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}
]})

Upvotes: 0

Views: 806

Answers (1)

Rober
Rober

Reputation: 726

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"
      }
    }
  ]

})

Upvotes: 1

Related Questions