Concat arrays using PyMongo failed with unknown group operator '$concatArrays'












0














I have mongodb data like:



{'word': 'good', 'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3}]}
{'word': 'spark', 'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10}]}
{'word': 'good', 'info': [{'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
{'word': 'spark', 'info': [{'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}


and I want to use pymongo to process it, the result should be:



{'word': 'good',
'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3},
{'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
{'word': 'spark',
'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10},
{'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}


I use group in pymongo:



a = mycol.aggregate([{"$group": {"_id":"$word", 'infos': {"$concatArrays": 1}}}])
for i in a:
print(i)


It went wrong: pymongo.errors.OperationFailure: unknown group operator '$concatArrays'.
and I use group keyword:



a = mycol.group(key='word',condition=None, initial={'infos': }, reduce={"$concatArrays": "info"})
for i in a:
print(i)


It also went wrong:



Traceback (most recent call last):File "F:/programs/SearchEngine/test.py", line 167, in <module> a = mycol.group(key='word',condition=None, initial={'infos': }, reduce={"$concatArrays": "info"})  File "C:Usersll.virtualenvsSearchEnginelibsite-packagespymongocollection.py", line 2550, in group  group["$reduce"] = Code(reduce)  File "C:Usersll.virtualenvsSearchEnginelibsite-packagesbsoncode.py", line 54, in __new__  "instance of %s" % (string_type.__name__))
TypeError: code must be an instance of str









share|improve this question





























    0














    I have mongodb data like:



    {'word': 'good', 'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3}]}
    {'word': 'spark', 'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10}]}
    {'word': 'good', 'info': [{'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
    {'word': 'spark', 'info': [{'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}


    and I want to use pymongo to process it, the result should be:



    {'word': 'good',
    'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3},
    {'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
    {'word': 'spark',
    'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10},
    {'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}


    I use group in pymongo:



    a = mycol.aggregate([{"$group": {"_id":"$word", 'infos': {"$concatArrays": 1}}}])
    for i in a:
    print(i)


    It went wrong: pymongo.errors.OperationFailure: unknown group operator '$concatArrays'.
    and I use group keyword:



    a = mycol.group(key='word',condition=None, initial={'infos': }, reduce={"$concatArrays": "info"})
    for i in a:
    print(i)


    It also went wrong:



    Traceback (most recent call last):File "F:/programs/SearchEngine/test.py", line 167, in <module> a = mycol.group(key='word',condition=None, initial={'infos': }, reduce={"$concatArrays": "info"})  File "C:Usersll.virtualenvsSearchEnginelibsite-packagespymongocollection.py", line 2550, in group  group["$reduce"] = Code(reduce)  File "C:Usersll.virtualenvsSearchEnginelibsite-packagesbsoncode.py", line 54, in __new__  "instance of %s" % (string_type.__name__))
    TypeError: code must be an instance of str









    share|improve this question



























      0












      0








      0







      I have mongodb data like:



      {'word': 'good', 'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3}]}
      {'word': 'spark', 'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10}]}
      {'word': 'good', 'info': [{'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
      {'word': 'spark', 'info': [{'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}


      and I want to use pymongo to process it, the result should be:



      {'word': 'good',
      'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3},
      {'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
      {'word': 'spark',
      'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10},
      {'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}


      I use group in pymongo:



      a = mycol.aggregate([{"$group": {"_id":"$word", 'infos': {"$concatArrays": 1}}}])
      for i in a:
      print(i)


      It went wrong: pymongo.errors.OperationFailure: unknown group operator '$concatArrays'.
      and I use group keyword:



      a = mycol.group(key='word',condition=None, initial={'infos': }, reduce={"$concatArrays": "info"})
      for i in a:
      print(i)


      It also went wrong:



      Traceback (most recent call last):File "F:/programs/SearchEngine/test.py", line 167, in <module> a = mycol.group(key='word',condition=None, initial={'infos': }, reduce={"$concatArrays": "info"})  File "C:Usersll.virtualenvsSearchEnginelibsite-packagespymongocollection.py", line 2550, in group  group["$reduce"] = Code(reduce)  File "C:Usersll.virtualenvsSearchEnginelibsite-packagesbsoncode.py", line 54, in __new__  "instance of %s" % (string_type.__name__))
      TypeError: code must be an instance of str









      share|improve this question















      I have mongodb data like:



      {'word': 'good', 'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3}]}
      {'word': 'spark', 'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10}]}
      {'word': 'good', 'info': [{'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
      {'word': 'spark', 'info': [{'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}


      and I want to use pymongo to process it, the result should be:



      {'word': 'good',
      'info': [{'tbl_id': 'd1', 'term_freq': 2}, {'tbl_id': 'd2', 'term_freq': 56}, {'tbl_id': 'd3', 'term_freq': 3},
      {'tbl_id': 'd4', 'term_freq': 12}, {'tbl_id': 'd5', 'term_freq': 8}, {'tbl_id': 'd8', 'term_freq': 7}]}
      {'word': 'spark',
      'info': [{'tbl_id': 'd1', 'term_freq': 6}, {'tbl_id': 'd3', 'term_freq': 11}, {'tbl_id': 'd4', 'term_freq': 10},
      {'tbl_id': 'd5', 'term_freq': 6}, {'tbl_id': 'd6', 'term_freq': 11}, {'tbl_id': 'd7', 'term_freq': 10}]}


      I use group in pymongo:



      a = mycol.aggregate([{"$group": {"_id":"$word", 'infos': {"$concatArrays": 1}}}])
      for i in a:
      print(i)


      It went wrong: pymongo.errors.OperationFailure: unknown group operator '$concatArrays'.
      and I use group keyword:



      a = mycol.group(key='word',condition=None, initial={'infos': }, reduce={"$concatArrays": "info"})
      for i in a:
      print(i)


      It also went wrong:



      Traceback (most recent call last):File "F:/programs/SearchEngine/test.py", line 167, in <module> a = mycol.group(key='word',condition=None, initial={'infos': }, reduce={"$concatArrays": "info"})  File "C:Usersll.virtualenvsSearchEnginelibsite-packagespymongocollection.py", line 2550, in group  group["$reduce"] = Code(reduce)  File "C:Usersll.virtualenvsSearchEnginelibsite-packagesbsoncode.py", line 54, in __new__  "instance of %s" % (string_type.__name__))
      TypeError: code must be an instance of str






      python mongodb aggregation-framework pymongo






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 29 '18 at 5:36









      styvane

      35.1k1277101




      35.1k1277101










      asked Dec 28 '18 at 2:56









      littlely

      254314




      254314
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The reason you are getting this error message is because the $concatArrays operator is an expression operator not a $group accumulator.



          That being said, you can do this with the following pipeline:



          [
          {
          "$group": {
          "_id": "$word",
          "info": {
          "$push": "$info"
          }
          }
          },
          {
          "$project": {
          "_id": 0,
          "word": "$_id",
          "info": {
          "$reduce": {
          "input": "$info",
          "initialValue": [

          ],
          "in": {
          "$concatArrays": [
          "$$value",
          "$$this"
          ]
          }
          }
          }
          }
          }
          ]


          We create a 2d list of info in the $group stage with the $push operator then in the another $project stage you flatten the list using the $reduce and $concatArrays.






          share|improve this answer





















            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%2f53953185%2fconcat-arrays-using-pymongo-failed-with-unknown-group-operator-concatarrays%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














            The reason you are getting this error message is because the $concatArrays operator is an expression operator not a $group accumulator.



            That being said, you can do this with the following pipeline:



            [
            {
            "$group": {
            "_id": "$word",
            "info": {
            "$push": "$info"
            }
            }
            },
            {
            "$project": {
            "_id": 0,
            "word": "$_id",
            "info": {
            "$reduce": {
            "input": "$info",
            "initialValue": [

            ],
            "in": {
            "$concatArrays": [
            "$$value",
            "$$this"
            ]
            }
            }
            }
            }
            }
            ]


            We create a 2d list of info in the $group stage with the $push operator then in the another $project stage you flatten the list using the $reduce and $concatArrays.






            share|improve this answer


























              0














              The reason you are getting this error message is because the $concatArrays operator is an expression operator not a $group accumulator.



              That being said, you can do this with the following pipeline:



              [
              {
              "$group": {
              "_id": "$word",
              "info": {
              "$push": "$info"
              }
              }
              },
              {
              "$project": {
              "_id": 0,
              "word": "$_id",
              "info": {
              "$reduce": {
              "input": "$info",
              "initialValue": [

              ],
              "in": {
              "$concatArrays": [
              "$$value",
              "$$this"
              ]
              }
              }
              }
              }
              }
              ]


              We create a 2d list of info in the $group stage with the $push operator then in the another $project stage you flatten the list using the $reduce and $concatArrays.






              share|improve this answer
























                0












                0








                0






                The reason you are getting this error message is because the $concatArrays operator is an expression operator not a $group accumulator.



                That being said, you can do this with the following pipeline:



                [
                {
                "$group": {
                "_id": "$word",
                "info": {
                "$push": "$info"
                }
                }
                },
                {
                "$project": {
                "_id": 0,
                "word": "$_id",
                "info": {
                "$reduce": {
                "input": "$info",
                "initialValue": [

                ],
                "in": {
                "$concatArrays": [
                "$$value",
                "$$this"
                ]
                }
                }
                }
                }
                }
                ]


                We create a 2d list of info in the $group stage with the $push operator then in the another $project stage you flatten the list using the $reduce and $concatArrays.






                share|improve this answer












                The reason you are getting this error message is because the $concatArrays operator is an expression operator not a $group accumulator.



                That being said, you can do this with the following pipeline:



                [
                {
                "$group": {
                "_id": "$word",
                "info": {
                "$push": "$info"
                }
                }
                },
                {
                "$project": {
                "_id": 0,
                "word": "$_id",
                "info": {
                "$reduce": {
                "input": "$info",
                "initialValue": [

                ],
                "in": {
                "$concatArrays": [
                "$$value",
                "$$this"
                ]
                }
                }
                }
                }
                }
                ]


                We create a 2d list of info in the $group stage with the $push operator then in the another $project stage you flatten the list using the $reduce and $concatArrays.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 29 '18 at 5:36









                styvane

                35.1k1277101




                35.1k1277101






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f53953185%2fconcat-arrays-using-pymongo-failed-with-unknown-group-operator-concatarrays%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

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'