Mongo查找多个匹配的对象内部和对象数组

时间:2016-01-28 05:24:07

标签: node.js mongodb meteor mongodb-query

我正在做的是列出客户在页面上输入的所有文件。然后,我有多个搜索框,可以用来缩小文档范围。每个搜索框都绑定到一个字段。我希望能够在键入时搜索并缩小结果范围。我需要搜索的一个字段作为包含其他对象的数组。如果他们搜索的字段位于对象的底部但是我找不到搜索对象数组的方法,那么我可以成功完成此操作。完整对象的示例如下。

{
"_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')}}}
        ]};

我尝试了很多东西,似乎都没有用。非常感谢他指出正确的方向。

1 个答案:

答案 0 :(得分: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')}}} }
        ]}
)