0

I have an application that simply takes and converts XML files to JSON and inserts them into a Mongo database. Inside each document of the collection I have an array VehicleEntry. Some of these VehicleEntries will have a tag called Pre-FlashDTC-IPCand I would like to pull all of those entries. Since I want the individual entries from the array, I used the unwind operator:

db.Vehicles.aggregate( [
    { $unwind : { path : "$VehicleEntry" } },
    { $match : { "$VehicleEntry.Pre-FlashDTC-IPC" : { $exists: true } } }
] );

I have tried this both with unwind first and match first, but neither work. I get an error:

{ "serverUsed": "localhost:27017", "ok": 0.0, "errmsg": "bad query: BadValue unknown top level operator: $VehicleEntry.Pre-FlashDTC-IPC", "code": 16810}

I thought the $exists operator would work to ensure I only returned elements that do have that value, but that doesn't appear to be the case. How can I correct this query?

Consider the following sample document:

{
    "VehicleEntry" : [
        {
            "BatteryStatus" : "GOOD",
            "Pre-FlashDTC-IPC" : "U100",
            "VehicleStatus" : "PASSED"
        }, 
        {
            "BatteryStatus" : "GOOD",
            "VehicleStatus" : "PASSED"
        }
    ],
    "project_id" : "1234"
}

There is some consistent information in the array, such as Battery and Vehicle status, but some have extra information like the first array item. I want to get the individual array items (hence the need for unwind) where this value exists. Therefore my expected results are:

{
    "BatteryStatus" : "GOOD",
    "Pre-FlashDTC-IPC" : "U100",
    "VehicleStatus" : "PASSED"
},
AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • Your question is hard to understand can you please show sample documents with the expected result? – styvane Dec 31 '15 at 16:32
  • @user3100115 hope my edit clarifies your concern. – AdamMc331 Dec 31 '15 at 16:49
  • Actually you don't need to `$unwind` your documents. Mongo provides a better way to do this. [Retrieve only the queried element in an object array in MongoDB collection](http://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – styvane Dec 31 '15 at 18:42
  • @user3100115 do you think that's more efficient than unwind? It doesn't really seem more readable to me. – AdamMc331 Dec 31 '15 at 19:24
  • Yes it's far more better than using `$unwind`, maybe author should have explained more. – styvane Dec 31 '15 at 19:25
  • $elemMatch projection can only be used if you're doing a find and it only returns the *first* matched element. Were you referring to $filter? It's not significantly more performant in most cases. – Asya Kamsky Dec 31 '15 at 19:39
  • The main difference appears to be the filter method in that accepted answer returns one array of all the matched items, where mine will return one document for each matched item in the array. The difference then is up to the developer, which ever is easier to work with. – AdamMc331 Dec 31 '15 at 19:44

1 Answers1

4

Omit the "$" in your match-clause

{ $match : { "VehicleEntry.Pre-FlashDTC-IPC" : { $exists: true } } }

The $ in front of a field-name is only needed when you want to access the value of a field in an aggregation operation.

Philipp
  • 67,764
  • 9
  • 118
  • 153