How to use PyMongo find() to search nested array attribute?












0















Using PyMongo, how would one find/search for the documents where the nested array json object matches a given string.



Given the following 2 Product JSON documents in a MongoDB collection..



[{
"_id" : ObjectId("5be1a1b2aa21bb3ceac339b0"),
"id" : "1",
"prod_attr" : [
{
"name" : "Branded X 1 Sneaker"
},
{
"hierarchy" : {
"dept" : "10",
"class" : "101",
"subclass" : "1011"
}
}
]
},
{
"_id" : ObjectId("7be1a1b2aa21bb3ceac339xx"),
"id" : "2",
"prod_attr" : [
{
"name" : "Branded Y 2 Sneaker"
},
{
"hierarchy" : {
"dept" : "10",
"class" : "101",
"subclass" : "2022"
}
}
]
}
]


I would like to
1. return all documents where prod_att.hierarchy.subclass = "2022"
2. return all documents where prod_attr.name contains "Sneaker"



I appreciate the JSON could be structured differently, unfortunately that is not within my control to change.










share|improve this question



























    0















    Using PyMongo, how would one find/search for the documents where the nested array json object matches a given string.



    Given the following 2 Product JSON documents in a MongoDB collection..



    [{
    "_id" : ObjectId("5be1a1b2aa21bb3ceac339b0"),
    "id" : "1",
    "prod_attr" : [
    {
    "name" : "Branded X 1 Sneaker"
    },
    {
    "hierarchy" : {
    "dept" : "10",
    "class" : "101",
    "subclass" : "1011"
    }
    }
    ]
    },
    {
    "_id" : ObjectId("7be1a1b2aa21bb3ceac339xx"),
    "id" : "2",
    "prod_attr" : [
    {
    "name" : "Branded Y 2 Sneaker"
    },
    {
    "hierarchy" : {
    "dept" : "10",
    "class" : "101",
    "subclass" : "2022"
    }
    }
    ]
    }
    ]


    I would like to
    1. return all documents where prod_att.hierarchy.subclass = "2022"
    2. return all documents where prod_attr.name contains "Sneaker"



    I appreciate the JSON could be structured differently, unfortunately that is not within my control to change.










    share|improve this question

























      0












      0








      0








      Using PyMongo, how would one find/search for the documents where the nested array json object matches a given string.



      Given the following 2 Product JSON documents in a MongoDB collection..



      [{
      "_id" : ObjectId("5be1a1b2aa21bb3ceac339b0"),
      "id" : "1",
      "prod_attr" : [
      {
      "name" : "Branded X 1 Sneaker"
      },
      {
      "hierarchy" : {
      "dept" : "10",
      "class" : "101",
      "subclass" : "1011"
      }
      }
      ]
      },
      {
      "_id" : ObjectId("7be1a1b2aa21bb3ceac339xx"),
      "id" : "2",
      "prod_attr" : [
      {
      "name" : "Branded Y 2 Sneaker"
      },
      {
      "hierarchy" : {
      "dept" : "10",
      "class" : "101",
      "subclass" : "2022"
      }
      }
      ]
      }
      ]


      I would like to
      1. return all documents where prod_att.hierarchy.subclass = "2022"
      2. return all documents where prod_attr.name contains "Sneaker"



      I appreciate the JSON could be structured differently, unfortunately that is not within my control to change.










      share|improve this question














      Using PyMongo, how would one find/search for the documents where the nested array json object matches a given string.



      Given the following 2 Product JSON documents in a MongoDB collection..



      [{
      "_id" : ObjectId("5be1a1b2aa21bb3ceac339b0"),
      "id" : "1",
      "prod_attr" : [
      {
      "name" : "Branded X 1 Sneaker"
      },
      {
      "hierarchy" : {
      "dept" : "10",
      "class" : "101",
      "subclass" : "1011"
      }
      }
      ]
      },
      {
      "_id" : ObjectId("7be1a1b2aa21bb3ceac339xx"),
      "id" : "2",
      "prod_attr" : [
      {
      "name" : "Branded Y 2 Sneaker"
      },
      {
      "hierarchy" : {
      "dept" : "10",
      "class" : "101",
      "subclass" : "2022"
      }
      }
      ]
      }
      ]


      I would like to
      1. return all documents where prod_att.hierarchy.subclass = "2022"
      2. return all documents where prod_attr.name contains "Sneaker"



      I appreciate the JSON could be structured differently, unfortunately that is not within my control to change.







      python json mongodb pymongo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 15:30









      user3927078user3927078

      294




      294
























          1 Answer
          1






          active

          oldest

          votes


















          0














          1. Return all documents where prod_attr.hierarchy.subclass = "2022"



          Based on the Query an Array of Embedded Documents documentation of MongoDB you can use dot notation concatenating the name of the array field (prod_attr), with a dot (.) and the name of the field in the nested document (hierarchy.subclass):



          collection.find({"prod_attr.hierarchy.subclass": "2022"})


          2. Return all documents where prod_attr.name contains "Sneaker"



          As before, you can use the dot notation to query a field of a nested element inside an array.
          To perform the "contains" query you have to use the $regex operator:



          collection.find({"prod_attr.name": {"$regex": "Sneaker"}})




          Another option is to use the MongoDB Aggregation framework:



          collection.aggregate([
          {"$unwind": "$prod_attr"},
          {"$match": {"prod_attr.hierarchy.subclass": "2022"}}
          ])


          the $unwind operator creates a new object for each object inside the prod_attr array, so you will have only nested documents and no array (check the documentation for details).

          The next step is the $match operator that actually perform a query on the nested object.



          This is a simple example but playing with the Aggregators Operators you have a lot of flexibility.






          share|improve this answer


























          • great response - thanks

            – user3927078
            Nov 30 '18 at 11:20











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415404%2fhow-to-use-pymongo-find-to-search-nested-array-attribute%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          1. Return all documents where prod_attr.hierarchy.subclass = "2022"



          Based on the Query an Array of Embedded Documents documentation of MongoDB you can use dot notation concatenating the name of the array field (prod_attr), with a dot (.) and the name of the field in the nested document (hierarchy.subclass):



          collection.find({"prod_attr.hierarchy.subclass": "2022"})


          2. Return all documents where prod_attr.name contains "Sneaker"



          As before, you can use the dot notation to query a field of a nested element inside an array.
          To perform the "contains" query you have to use the $regex operator:



          collection.find({"prod_attr.name": {"$regex": "Sneaker"}})




          Another option is to use the MongoDB Aggregation framework:



          collection.aggregate([
          {"$unwind": "$prod_attr"},
          {"$match": {"prod_attr.hierarchy.subclass": "2022"}}
          ])


          the $unwind operator creates a new object for each object inside the prod_attr array, so you will have only nested documents and no array (check the documentation for details).

          The next step is the $match operator that actually perform a query on the nested object.



          This is a simple example but playing with the Aggregators Operators you have a lot of flexibility.






          share|improve this answer


























          • great response - thanks

            – user3927078
            Nov 30 '18 at 11:20
















          0














          1. Return all documents where prod_attr.hierarchy.subclass = "2022"



          Based on the Query an Array of Embedded Documents documentation of MongoDB you can use dot notation concatenating the name of the array field (prod_attr), with a dot (.) and the name of the field in the nested document (hierarchy.subclass):



          collection.find({"prod_attr.hierarchy.subclass": "2022"})


          2. Return all documents where prod_attr.name contains "Sneaker"



          As before, you can use the dot notation to query a field of a nested element inside an array.
          To perform the "contains" query you have to use the $regex operator:



          collection.find({"prod_attr.name": {"$regex": "Sneaker"}})




          Another option is to use the MongoDB Aggregation framework:



          collection.aggregate([
          {"$unwind": "$prod_attr"},
          {"$match": {"prod_attr.hierarchy.subclass": "2022"}}
          ])


          the $unwind operator creates a new object for each object inside the prod_attr array, so you will have only nested documents and no array (check the documentation for details).

          The next step is the $match operator that actually perform a query on the nested object.



          This is a simple example but playing with the Aggregators Operators you have a lot of flexibility.






          share|improve this answer


























          • great response - thanks

            – user3927078
            Nov 30 '18 at 11:20














          0












          0








          0







          1. Return all documents where prod_attr.hierarchy.subclass = "2022"



          Based on the Query an Array of Embedded Documents documentation of MongoDB you can use dot notation concatenating the name of the array field (prod_attr), with a dot (.) and the name of the field in the nested document (hierarchy.subclass):



          collection.find({"prod_attr.hierarchy.subclass": "2022"})


          2. Return all documents where prod_attr.name contains "Sneaker"



          As before, you can use the dot notation to query a field of a nested element inside an array.
          To perform the "contains" query you have to use the $regex operator:



          collection.find({"prod_attr.name": {"$regex": "Sneaker"}})




          Another option is to use the MongoDB Aggregation framework:



          collection.aggregate([
          {"$unwind": "$prod_attr"},
          {"$match": {"prod_attr.hierarchy.subclass": "2022"}}
          ])


          the $unwind operator creates a new object for each object inside the prod_attr array, so you will have only nested documents and no array (check the documentation for details).

          The next step is the $match operator that actually perform a query on the nested object.



          This is a simple example but playing with the Aggregators Operators you have a lot of flexibility.






          share|improve this answer















          1. Return all documents where prod_attr.hierarchy.subclass = "2022"



          Based on the Query an Array of Embedded Documents documentation of MongoDB you can use dot notation concatenating the name of the array field (prod_attr), with a dot (.) and the name of the field in the nested document (hierarchy.subclass):



          collection.find({"prod_attr.hierarchy.subclass": "2022"})


          2. Return all documents where prod_attr.name contains "Sneaker"



          As before, you can use the dot notation to query a field of a nested element inside an array.
          To perform the "contains" query you have to use the $regex operator:



          collection.find({"prod_attr.name": {"$regex": "Sneaker"}})




          Another option is to use the MongoDB Aggregation framework:



          collection.aggregate([
          {"$unwind": "$prod_attr"},
          {"$match": {"prod_attr.hierarchy.subclass": "2022"}}
          ])


          the $unwind operator creates a new object for each object inside the prod_attr array, so you will have only nested documents and no array (check the documentation for details).

          The next step is the $match operator that actually perform a query on the nested object.



          This is a simple example but playing with the Aggregators Operators you have a lot of flexibility.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 16:29

























          answered Nov 21 '18 at 16:10









          vinscevinsce

          14511




          14511













          • great response - thanks

            – user3927078
            Nov 30 '18 at 11:20



















          • great response - thanks

            – user3927078
            Nov 30 '18 at 11:20

















          great response - thanks

          – user3927078
          Nov 30 '18 at 11:20





          great response - thanks

          – user3927078
          Nov 30 '18 at 11:20




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415404%2fhow-to-use-pymongo-find-to-search-nested-array-attribute%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]