Quantcast
Channel: Active questions tagged mongodb-atlas - Stack Overflow
Viewing all articles
Browse latest Browse all 271

pipeline with only simple $lookup stage automatically unwinds the data in the root place without using $unwind in pymongo python, is it possible?

$
0
0

I have two collections "users" and "persons", the "users" collection has an ObjectId of the "persons" collection ("person_id: "ObjectId("some_string")", I'm trying to get all users with the related data from the "persons" collection.Although I used a simple $lookup, unfortunately, I got an inappropriate result

  1. My pipeline is:
pipeline = [    {"$lookup": {"from": "persons","localField": "person_id","foreignField": "_id","as": "person_lookup_data"        }    }]
  1. My users collection is:
[    {"_id": { "$oid": "66447289442c0747d99f53d2" },"person_id": { "$oid": "66447288442c0747d99f53d1" },"role": "regular_r","created_at": { "$date": { "$numberLong": "1715761801362" } },"updated_at": { "$date": { "$numberLong": "1715761801362" } }    },    {"_id": { "$oid": "66450e6a2ece17f018ddbe65" },"person_id": { "$oid": "66450e692ece17f018ddbe64" },"role": "regular_r","created_at": { "$date": { "$numberLong": "1715801706337" } },"updated_at": { "$date": { "$numberLong": "1715801706337" } }    }]
  1. My persons collection is:
[    {"_id": { "$oid": "66447288442c0747d99f53d1" },"first_name": "John","last_name": "Doe","email": "a@b.com","password": "123456","phone_number": "0529248906","alternative_number": "0987654321","address": { "$oid": "66447287442c0747d99f53d0" },"gender": "Male","birthday": { "$date": { "$numberLong": "376444800000" } },"profile_image": "some image","profile_icon": "some icon","active": true,"added_by": "normal","created_at": { "$date": { "$numberLong": "1715761800275" } },"updated_at": { "$date": { "$numberLong": "1715761800275" } }    },    {"_id": { "$oid": "66450e692ece17f018ddbe64" },"first_name": "Dana","last_name": "Levi","email": "dana@levi.com","password": "123456","phone_number": "0529248906","alternative_number": "0987654321","address": { "$oid": "66450e672ece17f018ddbe63" },"gender": "Female","birthday": { "$date": { "$numberLong": "379296000000" } },"profile_image": "some image","profile_icon": "some icon","active": true,"added_by": "normal","created_at": { "$date": { "$numberLong": "1715801705281" } },"updated_at": { "$date": { "$numberLong": "1715801705281" } }    }]
  1. my aggregate function is:
    def aggregate(self, pipeline):        # Execute aggregation query        result = list(self.__db['users'].aggregate(pipeline))        print("result = ", result)        return result
  1. But unfortunately, this is the result:
    [        {"_id": "66447288442c0747d99f53d1","active": true,"added_by": "normal","address": "66447287442c0747d99f53d0","alternative_number": "0987654321","birthday": "1981-12-06T00:00:00","created_at": "2024-05-15T08:30:00.275000","email": "a@b.com","first_name": "John","gender": "Male","last_name": "Doe","password": "123456","person_lookup_data": [],"phone_number": "0529248906","profile_icon": "some icon","profile_image": "some image","updated_at": "2024-05-15T08:30:00.275000"        },        {"_id": "66450e692ece17f018ddbe64","active": true,"added_by": "normal","address": "66450e672ece17f018ddbe63","alternative_number": "0987654321","birthday": "1982-01-08T00:00:00","created_at": "2024-05-15T19:35:05.281000","email": "dana@levi.com","first_name": "Dana","gender": "Female","last_name": "Levi","password": "123456","person_lookup_data": [],"phone_number": "0529248906","profile_icon": "some icon","profile_image": "some image","updated_at": "2024-05-15T19:35:05.281000"        }    ]
  1. This is the expected result:
    [        {"_id": "66447289442c0747d99f53d2","created_at": "2024-05-15T08:30:01.362000","person_id": "66447288442c0747d99f53d1","person_lookup_data": [                {"_id": "66447288442c0747d99f53d1","active": true,"added_by": "normal","address": "66447287442c0747d99f53d0","alternative_number": "0987654321","birthday": "1981-12-06T00:00:00","created_at": "2024-05-15T08:30:00.275000","email": "a@b.com","first_name": "John","gender": "Male","last_name": "Doe","password": "123456","phone_number": "0529248906","profile_icon": "some icon","profile_image": "some image","updated_at": "2024-05-15T08:30:00.275000"                }            ],"role": "regular_r","updated_at": "2024-05-15T08:30:01.362000"        },        {"_id": "66450e6a2ece17f018ddbe65","created_at": "2024-05-15T19:35:06.337000","person_id": "66450e692ece17f018ddbe64","person_lookup_data": [                {"_id": "66450e692ece17f018ddbe64","active": true,"added_by": "normal","address": "66450e672ece17f018ddbe63","alternative_number": "0987654321","birthday": "1982-01-08T00:00:00","created_at": "2024-05-15T19:35:05.281000","email": "dana@levi.com","first_name": "Dana","gender": "Female","last_name": "Levi","password": "123456","phone_number": "0529248906","profile_icon": "some icon","profile_image": "some image","updated_at": "2024-05-15T19:35:05.281000"                }            ],"role": "regular_r","updated_at": "2024-05-15T19:35:06.337000"        }    ]

Please can someone help me?


Viewing all articles
Browse latest Browse all 271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>