0

I have few JSON documents stored in MongoDB that are basically error reports, i.e. each document contains a nested data structure mismatches between datasets. They look like the following:

    {
      "nodename": "BLAH"
      "errors": {
        "PC": {
          "value": {
            "PC93196": [
              "post",
              "casper"
            ],
            "PC03196": [
              "netdb"
            ]
          }
        }
      }
    }

So in the above case, a process/tool determines the PC value of PC93196 and PC03196. netdb in this case reported a different PC value to that of post and casper.

How do I query for all documents that have errors where the process/tool casper is involved?

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
yee379
  • 6,498
  • 10
  • 56
  • 101

1 Answers1

0

The document schema doesn't really lend itself to being queried very easily in the way you're trying to. This is because the value object is composed of named objects, rather than being an Array of objects. I.e. the value object has two properties: PC93196 and PC03196.

If your document was structured slightly differently it would be much easier to query in the way you want. For example a document structure like this:

{
  "nodename": "BLAH",
  "errors": {
    "PC": [
      {
        "name": "PC93196",
        "type": [
          "post",
          "casper"
        ]
      },
      {
        "name": "PC03196",
        "type": [
          "netdb"
        ]
      }
    ]
  }
}

Would allow you to write a query like this:

db.errors.aggregate([{$unwind:"$errors.PC"},{$match:{"errors.PC.type":"casper"}}])

which would provide a result:

{ "_id" : ObjectId("5763c480dad14fd061657f91"), "nodename" : "BLAH", "errors" : { "PC" : { "name" : "PC93196", "type" : [ "post", "casper" ] } } }
robjwilkins
  • 5,462
  • 5
  • 43
  • 59