2016-08-14 61 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" 
     } 
    } 
    ] 

})