Reputation: 250
What I am doing is having a list of all documents entered by the client on the page. Then I have multiple search boxes that they can use to narrow down the documents. Each search box is tied to a field. I want to be able to search and narrow down the results as they are typing. One of the fields that I need to search into as an array that hold other objects. I can successful accomplish this if the field(s) they are searching against are in the base of the object but I fail to find a way to search into the array of objects. An example of the complete object is below.
{
"_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"
}
]
}
So what I have tested is below. I am just trying to isolate a way to search multiple fields inside the orderDispatch right now then I will add back in other fields.
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')}}}
]};
I have tried multiple things and none seem to work. A point int he right direction would be greatly appreciated.
Upvotes: 2
Views: 1212
Reputation: 573
There is syntax error in your query :
Use following syntax for $elemMatch
query :
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')}}} }
]}
)
Upvotes: 1