How to find a specific value nested in an unknown object?

I am very new to MongoDB. However for the last 48 hours I managed to create a nice collection of Amazon products, mostly audio CDs and DVDs. The Products collection has approximately 50k documents in the following format:

      "_id": {
        "$oid": "5fbc2e9b8ca9b50a6c13fc56"
      "016581598027": [
          "ASIN": "B00008OM5J",
          "Language": "en-US",
          "AudienceRating": "NR (Not Rated)",
          "Binding": "Audio CD",
          "Creator": "Nothingface",
          "Edition": "Parental Advisory ed.",
          "Label": "TVT",

When I query Amazon MWS my program automatically saves query results to MongoDB.

The 016581598027 is a UPC (Universal Product Code) which may be referred to as a product identifier. Needless to say that each document in my collection is supposed to have a different UPC at this nest level. However I want to be able to query and find a document on a specific ASIN. As a pseudo-code what I want is this:


So after the above query fires I must have a reference to a sole document. How do I do that? I’ve tried everything – RTFM, talked to Julia bot, etc. but nothing helps.

To make it even more interesting for you, I’d like to extend the previous task with an OR query:

document = (mydb.Products.find({"ASIN":"B00008OM5J"}) OR mydb.Products.find("????":"016581598027"))

How do I implement this?


I do not have an answer to your question however I will risk a comment about your schema.

Your problem would be easier if you had a field named UPC with values like 016581598027.

Any reason why the field keyed with the UPC is an array? Is the indent to have multiple UPC object within each top level document?

Dear Steeve,

Thank you for your comment. I understand, that the proposed nesting (the UPC attribute is on the same level with the ASIN attribute) would be much easier. However I use a third-party PHP library which returns JSON in exactly this format.

Well, I can for sure do some coding to change the returned JSON to comply with what you mentioned, but: I am very curious how MongoDB can solve exactly this problem when

  • the UPC attribute is actually unnamed (doesn’t have a key);
  • it is one level up;
  • it varies for each document in my collection.

I am very curious how MongoDB can solve this.