Frage

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']})
War es hilfreich?

Lösung 2

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']})

Andere Tipps

The .find() returns a single iterator, a=b - assuming that's actually supposed to be b=a - just sets it up under a different name, both loops are consuming from the same in-memory object.

do a = db.col.find() and b = db.col.find() to get independent iterators.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top