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

4
  • You can use add operators between "arrays" (actually lists): ['red'] + ['blue' ] == ['red', 'blue']. You should first work through the Python tutorial if not done yet. Commented Dec 14, 2022 at 4: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? Commented Dec 14, 2022 at 4: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 Commented Dec 14, 2022 at 5: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. mongodb.com/community/forums/t/syntax-error-in-match/111945 Commented Dec 15, 2022 at 7:05

1 Answer 1

2

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]

Sign up to request clarification or add additional context in comments.

2 Comments

Please check full question I need to pass array merge as an object got error while passing the list in aggregation Pipeline
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.