0

I am trying to merge multiple arrays in python to make a single array just like array_merge() function in PHP

example: in PHP

$condition  = "";
$a1=array("red","green");
$a2=array("blue","yellow");
$condition = (array_merge($a1,$a2));
 //and output of $condition like -
Array ( [0] => red [1] => green [2] => blue [3] => yellow )

I have arrays in python -

qualification_filter = []
language_filter = []
conditionstoaggregate  = ""

if qualified == 1:
   qualification_filter = {"qualification": {"$gt": {"size": 0}}}

if len(language) > 0 and language[0] != "undefined":
   language_filter = {"language": {"$in": language}}


condition = {
  "rejected": {"$ne": "1"},
  "clientid": 22,
  "keyword.keytpe": {"$in": "finals"},
        }

How will I merge these 3 condition + language_filter + qualification_filter to make a single array conditionstoaggregate

which I need to merge and put in a single array conditionstoaggregate = "" also if any array is empty it shouldn't be added in the conditionstoaggregate = ""

then pass in a MongoDB aggregate as- match filter must be an expression in an object

aggregate = [
    {"$match": conditionstoaggregate},
    {
        "$lookup": {
            "from": "companyissue",
            "localField": "keyword.keyid",
            "foreignField": "issueid",
            "as": "issue_company",
        }
    },
]

The purpose of this question is I need to add all arrays in single dimensions and pass it as a parameter in the MongoDB aggregate function also if an array is empty it shouldn't be merged or passed! is there any way to do this?

after all your suggestion I tried but not help because your methods are for lists not object for reference see below I tried both method of list and itertools one by one.

  conditionstoaggregate = itertools.chain([condition], [language_filter], [qualification_filter])
     conditionstoaggregate = [condition] + [language_filter] + [qualification_filter]

    aggregate = [
    {"$match": conditionstoaggregate},
    {
        "$lookup": {
            "from": "companyissue",
            "localField": "keyword.keyid",
            "foreignField": "issueid",
            "as": "issue_company",
        }
    },
]

it gives an error in MongoDB aggregation -

pymongo.errors.OperationFailure: the match filter must be an expression in an object, full error: {'operationTime': Timestamp(1670994472, 1), 'ok': 0.0, 'errmsg': 'the match filter must be an expression in an object'

so I need to pass as an object in aggregation pipeline by merging all array into one

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • You can use add operators between "arrays" (actually lists): `['red'] + ['blue' ] == ['red', 'blue']`. You should first work through the [Python tutorial](https://docs.python.org/3/tutorial/) if not done yet. – Michael Butscher Dec 14 '22 at 04:45
  • For python, naive way is use `+` operator to concatenate the lists. Or better, `itertools` module: `conditionstoaggregate = itertools.chain(qualification_filter, language_filter, keyword_filter, news_category)`. [How do I merge multiple lists into one list?](https://stackoverflow.com/questions/11574195/how-do-i-merge-multiple-lists-into-one-list) – chickity china chinese chicken Dec 14 '22 at 04:45
  • @MichaelButscher I need to pass as an object in `aggregation`, not as a list or array error is this `the match filter must be an expression in an object` I have updated the question for more reference – Kunal Pandey Dec 14 '22 at 05:17
  • Have you researched this error outside of stackoverflow ? e.g. `You have to remove the square brackets from your $match stage, in order to make it an object instead of an array.` https://www.mongodb.com/community/forums/t/syntax-error-in-match/111945 – chickity china chinese chicken Dec 15 '22 at 07:05

1 Answers1

1

You can also use the extend method over a list

list1 = [1, 2]
list2 = [3, 4]
list1.extend(list2)
print(list1)

[1, 2, 3, 4]

  • Please check full question I need to pass array merge as an object got error while passing the list in aggregation Pipeline – Kunal Pandey Dec 14 '22 at 05:38
  • So, you dont want to merge lists, but dictionaries (json-like objects). Given a list of dictionaries, like `condition + language_filter + qualification_filter`, you can use the update function. `conditionstoaggregate={} ; all_conditions=[condition, language_filter, qualification_filter] ; for c in all_conditions: conditionstoaggregate.update(c)` – Alfonso Alvarenga Dec 15 '22 at 01:07