2016-01-28 66 views
2

我正在做的是獲取客戶端在頁面上輸入的所有文檔的列表。然後我有多個搜索框可以用來縮小文檔的範圍。每個搜索框都綁定到一個字段。我希望能夠在輸入時搜索並縮小搜索結果的範圍。我需要搜索的一個字段是一個包含其他對象的數組。如果他們正在搜索的字段位於對象的基礎中,但我無法找到搜索對象數組的方法,則可以成功完成此操作。下面是一個完整對象的例子。Mongo查找內部和對象數組的多個匹配

{ 
"_id" : "fNgY9TcdCnyEFwePT", 
"companyCode" : "demoAccount1", 
"tripNumber" : 5002, 
"tripNumberText" : "5002", 
"custID" : "TnfFBRt3bZvNrSRB7", 
"customerName" : "Am Transport Inc", 
"orderLoadNum" : "50021321", 
"orderPlacedDate" : ISODate("2016-01-27T08:00:00.000Z"), 
"orderDispatch" : [ 
    { 
     "Address" : "4111 Aurora Ave N", 
     "Appt" : "", 
     "Cases" : "", 
     "City" : "Seattle", 
     "Contact" : "", 
     "Customer" : "ABC 1", 
     "DatePuDel" : ISODate("2016-01-27T08:00:00.000Z"), 
     "Email" : "", 
     "Fax" : "", 
     "Notes" : "", 
     "ParsPaps" : "", 
     "Phone1" : "", 
     "Phone2" : "", 
     "Ref" : "", 
     "Skids" : "", 
     "State" : "WA", 
     "Suite" : "", 
     "Weight" : "", 
     "Zip" : "98103", 
     "carrierCharges" : "", 
     "carrierCurrency" : "CDN", 
     "carrierName" : "", 
     "driverName" : "", 
     "stop" : true, 
     "stopDelDate" : "", 
     "stopDelTime" : "", 
     "stopNum" : 0, 
     "stopPuDate" : "", 
     "stopPuTime" : "", 
     "stopType" : "pickup", 
     "trailerNum" : "", 
     "truckNum" : "", 
     "deliveryType" : "", 
     "deliveryStatus" : "Entered" 
    }, 
    { 
     "Address" : "8325 Main St", 
     "Appt" : "", 
     "Cases" : "", 
     "City" : "Vancouver", 
     "Contact" : "", 
     "Customer" : "ABC 2", 
     "DatePuDel" : ISODate("2016-01-27T08:00:00.000Z"), 
     "Email" : "", 
     "Fax" : null, 
     "Notes" : "", 
     "ParsPaps" : "", 
     "Phone1" : "", 
     "Phone2" : "", 
     "Ref" : "", 
     "Skids" : "", 
     "State" : "WA", 
     "Suite" : "", 
     "Weight" : "", 
     "Zip" : "V5X 3M3", 
     "carrierCharges" : "", 
     "carrierCurrency" : "CDN", 
     "carrierName" : "", 
     "driverName" : "Test Driver 1", 
     "stop" : false, 
     "stopDelDate" : "", 
     "stopDelTime" : "", 
     "stopNum" : 1, 
     "stopPuDate" : "", 
     "stopPuTime" : "", 
     "stopType" : "delivery", 
     "trailerNum" : 4412, 
     "truckNum" : 101, 
     "stopMiles" : "182.2", 
     "deliveryType" : "Highway", 
     "deliveryStatus" : "Dispatched", 
     "truckNumText" : "101", 
     "trailerNumText" : "4412" 
    } 
    ] 
} 

所以我測試了下面。我只是試圖隔離一種方法來搜索orderDispatch中的多個字段,然後我將添加回其他字段。

 query = {"$and": [ 
      { "$elemMatch" :{"orderDispatch.trailerNumText": {$regex: new RegExp('^' + $('input:text[name=traceBoardTrailerNumSetting]').val(), 'i')}}}, 
      { "$elemMatch" :{"orderDispatch.carrierName": {$regex: new RegExp('^' + $('input:text[name=traceBoardCarrierSetting]').val(), 'i')}}}, 
      { "$elemMatch" :{"orderDispatch.truckNumText": {$regex: new RegExp('^' + $('input:text[name=traceBoardTruckSetting]').val(), 'i')}}} 
     ]}; 

我已經嘗試了多種事情,似乎都沒有工作。我們非常感謝他的正確方向。

+0

爲什麼和''的$代替'$或'搜索多個字段時?不太可能,他們都會同時包含相同的模式嗎? –

+0

這就是要點。這將允許用戶通過檢查多個字段來大大縮小搜索範圍。如果我使用或然後返回的數據將不會被分區 – BobFranz

回答

1

。在你的查詢語法錯誤:

使用以下語法$elemMatch查詢:

db.collectionName.find(
    {"$and": [ 
      { "orderDispatch" : { "$elemMatch" :{ "trailerNumText": {$regex: new RegExp('^' + 'insert your value here', 'i')}}} }, 
      { "orderDispatch" : { "$elemMatch" :{ "carrierName": {$regex: new RegExp('^' + 'insert your value here', 'i')}}} }, 
      { "orderDispatch" : { "$elemMatch" :{ "truckNumText": {$regex: new RegExp('^' + 'insert your value here', 'i')}}} } 
     ]} 
) 
相關問題