Skip to content

Instantly share code, notes, and snippets.

@yannick
Created March 16, 2011 21:26
Show Gist options
  • Select an option

  • Save yannick/873350 to your computer and use it in GitHub Desktop.

Select an option

Save yannick/873350 to your computer and use it in GitHub Desktop.
mongodb cursors on indexes and jumping
#context: i want to mergejoin two generators/cursors by a key of a mongo document
#assuming an compund index af "keyA":1,"keyB":1,"keyC":1 exists.
#this is how i do it
cursor = self.collection.find( { "keyA":"foo", "keyB":"bar" }, { "keyC":1})
nextid = None
while 1:
mongo_doc = cursor.next()
result = mongo_doc["keyC"]
if nextid and nextid > result:
mongo_doc.update( {"keyC":{"$gte":nextid}})
cursor = self.collection.find(mongo_doc, {"keyC":1})
#this is where my merge join operation sends back the next possible lowest key
nextid = yield( result)
#this is how it should look imho
cursor = self.collection.find( { "keyA":"foo", "keyB":"bar" }, { "keyC":1})
for mongo_doc in cursor:
result = mongo_doc["keyC"]
nextid = yield( result)
if nextid:
cursor.jump_to( {"keyC":{"$gte":nextid}} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment