Bob Tway
Bob Tway

Reputation: 9613

Native Querying MongoDB inside arrays

I have a monogoDB which contains data in the following structure:

{   "Locales" : {
    "Invariant" : {
      "Checksum" : "7B4C15B697AAFC1484679076667267D2",
      "Metadata" : {
        "app" : {
          "@appType" : "app",
          "@id" : "284896113",
          "categories" : {
            "category" : [{
                "@id" : "6011",
                "@parentid" : "36",
                "@type" : "GENRE",
                "##text##" : "Music"
              }, {
                "@id" : "6016",
                "@parentid" : "36",
                "@type" : "GENRE",
                "##text##" : "Entertainment"
              }],
            "##category" : null
          },
          "rating" : "2.5",
          }
        }
      },
      "ModifiedDate" : ISODate("2012-09-07T09:07:58.218Z")
    }   },   "_id" : "selection/live/app(284896113)"

There's about 100,000 of them in my database. I need to find out several things. Firstly how many of them have the "categories" array, then how many of those have one or more "category" array underneath them and finally, of those, how many have have a value for ##text##?

Cheers

Upvotes: 2

Views: 95

Answers (1)

Thilo
Thilo

Reputation: 262714

You can use $exists:

db.things.count( { 
   'Locales.Invariant.Metadata.app.categories': {$exists: true }} );

db.things.count( { 
   'Locales.Invariant.Metadata.app.categories.category': {$exists: true }} );

db.things.count( { 
   'Locales.Invariant.Metadata.app.categories.category.##text##': 
      {$exists: true }} );

Note that this will return the count of documents, not the count of matches. In your example, it will return '1' because there is one document, not '2' (for the two occurrences of ##text##).

Upvotes: 2

Related Questions