Search code examples
pythonmongodbpymongoedges

Create edges database based on similar id


I have a mondgodb used with pymongo and a entry looks like:

{ "_id" : ObjectId( "52065432c36b44162f56f4a7" ),
  "user_id" : "22614761",
  "longitude" : 25.394902576,
  "created_time" : "1376144888",
  "latitude" : 36.476277607,
  "id" : "519463433050680017_22614761" }

I want to find all data that have similar user_id and store their id in a new database. E.g. one newly created entry:

{"target":"519463433050680017_22614761", "source" : "518989990404955532_361611158"}

I tried the following code but the outer loop stucks to the first value.

a = db.col.find()
b = db.col.find()
for i in a:
    for q in b:
        if i['_id'] <>  q['_id'] and i['user_id'] == q['user_id']:
            edges.insert({'source':i['user_id'],'target': q['user_id']})

Solution

  • Alright I was able to solve it with the following code. The a and b were pymongo cursors which for some reason (I would be glad to hear if somebody knows) didn't behave as dictionaries.

    a = db.col.find()
    bjects = []
    for object in a:
           objects.append(object)
    
    
    for i in objects:
        for q in objects:
            if i['_id'] <>  q['_id'] and i['user_id'] == q['user_id']:
                edges.insert({'source':i['id'],'target': q['id']})