2

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.

2
  • Why $and instead of $or when searching multiple fields? Unlikely they would all contain the same pattern at the same time no? Commented Jan 28, 2016 at 5:57
  • That is the point. This would allow the use to greatly narrow his search by being able have multiple fields checked against. If I use or then the returned data will not be as zoned in Commented Jan 28, 2016 at 5:58

1 Answer 1

1

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')}}} }
        ]}
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.