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-IPC
and 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"
},