Hi every one. I’m new with mongo and pymongo. i want to extract the subscription between two array : randomlist and randomlist2. how can i see the result of my query? (it is a cursor type and i wanna see the result in a list form). thank you for your tips in advance
my code:
mylist = [
{"_id":1, "set":randomlist},
{ "_id":3, "set":randomlist2},
]
mycol.insert_many(mylist)
a=mydb.customer.aggregate([
{ "$match": { "_id": { "$in": [1, 3] } } },
{
"$group": {
"_id": 0,
"sets": { "$push": "$set" },
"initialSet": { "$first": "$set" }
}
},
{
"$project": {
"commonSets": {
"$reduce": {
"input": "$sets",
"initialValue": "$initialSet",
"in": { "$setIntersection": ["$$value", "$$this"] }
}
}
}
}
])
print(type(cursor))
for doc in mydb.customer.aggregate(list(a)):
print(doc)
Hi @elmira_naseh
Welcome to MongoDB community!
I guess you can use pprint and list methods just as mentioned here :
https://api.mongodb.com/python/current/examples/aggregation.html#aggregation-framework
pprint( list(mydb.customer.aggregate([
{ "$match": { "_id": { "$in": [1, 3] } } },
{
"$group": {
"_id": 0,
"sets": { "$push": "$set" },
"initialSet": { "$first": "$set" }
}
},
{
"$project": {
"commonSets": {
"$reduce": {
"input": "$sets",
"initialValue": "$initialSet",
"in": { "$setIntersection": ["$value", "$this"] }
}
}
}
}
])));
Thanks
Pavel
Pavel_Duchovny:
pprint( list(mydb.customer.aggregate([
{ "$match": { "_id": { "$in": [1, 3] } } },
{
"$group": {
"_id": 0,
"sets": { "$push": "$set" },
"initialSet": { "$first": "$set" }
}
},
{
"$project": {
"commonSets": {
"$reduce": {
"input": "$sets",
"initialValue": "$initialSet",
"in": { "$setIntersection": ["$value", "$this"] }
}
}
}
}
])))
thanks for your response. it does not work, it just return . but this two lists have common items
Hi @elmira_naseh ,
I cannot comment on the correctness of the query.
If you wish me to comment on that please provide the data set you run this query on and the driver version.
Best
Pavel
Pavel_Duchovny:
driver version
it is 3.8.0
all of my data is here, i want to know the intersection between allusers and user_contacts collection:
import pymongo
import random
import pprint
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["test"]
user_contacts = mydb["user_contacts"]
randomlist2 = random.sample(range(10000000000, 99999999999), 500)
c=[12345678910,123456, 65824985291, 80787154324, 34935414117, 46032157504]
randomlist2.extend(c)
final=[]
for i in range(len(randomlist2)):
result_dict2={"tel1":randomlist2[i]}
final.append(result_dict2)
user_contacts.insert_many(final)
all_users = mydb["all_users"]
randomlist = random.sample(range(10000000000, 99999999999), 1000000)
final2=[]
for i in range(len(randomlist)):
result_dict={"tel2":randomlist[i]}
final2.append(result_dict)
all_users.insert_many(final2)