2016-08-14 91 views
0

我正在建立自己的查询与npm mongo-query-generator和我在报价条件。有什么办法删除这些引号,以便我可以正确地应用正则表达式吗?Mongo正则表达式查询删除引号

我目前有:

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

我想要什么:

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

你确定你需要删除引号?有[另一种方式](http://stackoverflow.com/a/494046/6083675)在JS中使用正则表达式。 – Laurel

+0

我目前使用正则表达式的方法,但我可以通过使用mongo $正则表达式保留引号 – Rober

回答

0

这是我发现的方式来保持引号:

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

})