2015-04-17 48 views
0

Ex。記錄MongoDB Aggregate正則表達式匹配或全文搜索返回整個文檔

[ 
    { 
    "_id": "5528cfd2e71144e020cb6494", 
    "__v": 11, 
    "Product": [ 
       { 
       "_id": "5528cfd2e71144e020cb6495", 
       "isFav": true, 
       "quantity": 27, 
       "price": 148, 
       "description": "100g", 
       "brand": "JaldiLa", 
       "name": "Grapes", 
       "sku": "GRP" 
       }, 
       { 
       "_id": "552963ed63d867b81e18d357", 
       "isFav": false, 
       "quantity": 13, 
       "price": 290, 
       "description": "100g", 
       "brand": "JaldiLa", 
       "name": "Apple", 
       "sku": "APL" 
       } 
      ], 
    "brands": [ 
      "Whole Foods", 
      "Costco", 
      "Bee's", 
      "Masons" 
      ], 
    "sku": "FRT", 
    "name": "Fruits" 
    } 
] 

我的貓鼬函數從AngularJS返回查詢(http://localhost:8080/api/search?s=

router.route('/search') 
    .get(function(req, res) { 
     Dept.aggregate(
       { $match: { $text: { $search: req.query.s } } }, 
       { $project : { name : 1, _id : 1, 'Product.name' : 1, 'Product._id' : 1} }, 
       { $unwind : "$Product" }, 
       { $group : { 
        _id : "$_id", 
        Category : { $addToSet : "$name"}, 
        Product : { $push : "$Product"} 
       }} 
     ) 
    }); 

結果:例如http://localhost:8080/api/search?s=Apple /葡萄/胡蘿蔔,結果全部相同。

[ 
    { 
    "_id": "5528cfd2e71144e020cb6494", 
    "Category": ["Fruits"], 
    "Product": [ 
       { 
       "_id": "5528cfd2e71144e020cb6495", 
       "name": "Grapes" 
       }, 
       { 
       "_id": "552963ed63d867b81e18d357", 
       "name": "Apple" 
       }, 
       { 
       "_id": "552e61920c530fb848c61510", 
       "name": "Carrots" 
       } 
      ] 
    } 
]  

問題:在「蘋果」的查詢,它返回的產品,而不是僅僅「葡萄」中的所有對象,我想也許把比賽的展開後會做的伎倆或$正則表達式的情況下

什麼我想:例如對於「葡萄」searchString 另外我希望它開始發送結果,只要我發送我的查詢的前兩個字母。

[{ 
    "_id": ["5528cfd2e71144e020cb6494"], //I want this in array as it messes my loop up 
    "Category": "Fruits", //Yes I do not want this in array like I'm getting in my resutls 
    "Product": [{ 
       "_id": "5528cfd2e71144e020cb6495", 
       "name": "Grapes" 
       }] 
}] 

感謝您的耐心。

+0

UPDATE:{$比賽:{ '$ idol.name':新的RegExp(QUER y,'gi')}}通過鍵查詢給出一個鍵,現在剩下的東西是特定的對象只返回 –

+0

UPDATE:{$ sort:{score:{$ meta:「textScore」},name:1}},{ $ limit:1}它返回整個文檔 –

+2

請創建一個[Minimal,Complete,Verifiable Example](http://stackoverflow.com/help/mcve) –

回答

1

使用下聚合管線:

var search = "apple", 
    pipeline = [ 
     { 
      "$match": { 
       "Product.name": { "$regex": search, "$options": "i" } 
      } 
     }, 
     { 
      "$unwind": "$Product" 
     }, 
     { 
      "$match": { 
       "Product.name": { "$regex": search, "$options": "i" } 
      } 
     }, 
     { 
      "$project": { 
       "Category": "$name", 
       "Product._id": 1, 
       "Product.name": 1 
      } 
     } 
    ]; 

    db.collection.aggregate(pipeline); 

利用上述示例文檔和一個正則表達式(不區分大小寫)搜索「蘋果」的產品陣列的name字段,上述聚合管線產生結果:

輸出

/* 1 */ 
{ 
    "result" : [ 
     { 
      "_id" : "5528cfd2e71144e020cb6494", 
      "Product" : { 
       "_id" : "552963ed63d867b81e18d357", 
       "name" : "Apple" 
      }, 
      "Category" : "Fruits" 
     } 
    ], 
    "ok" : 1 
} 
+1

非常感謝,所以$ group不是必需的.wish我知道早些時候...可能已經救了我3天 –

+1

@AyushSingh當你想根據特定字段對文檔進行分組併合並它們的值時,這是必要的。在你的情況下,你不需要任何分組,而是根據一定的條件在某些領域進行一些投影。 – chridam

相關問題