-1


I try to write a webapp with python framework Flask and MongoDB, using pymongo driver.
I need to get an admin view so I'm being able to edit the data online,

The structure of a collection :

db.skills.insert({ "rubric": rubric, "head": title, "body": content })

(Each field can be $unset if the value is < 0)

In the controller I send the data to the view by creating a list of dict :

def skList():
    liste, keys, value = [], [], []
# iterate over the collection to get 2 list of all keys and values.
    for row in db.skills.find():
        keys.append(row.keys())
        value.append(row.values())
# merge the 2 lists.
    zipped = zip(keys, value)
# finally create a list of dict containing each row of my db
    for x, y in zipped:
        liste.append(dict(zip(x, y)))
    return liste

The result is something like that :

liste = [ {_id:ObjectId('89878979879'), 'rubric':'Python', 'body': 'truc'}, {_id:ObjectId('876876899'), 'rubric':'chose', 'head':'blabla', 'body':'foo'} ]

In my view I iterate over and it displays fine !
Also I can edit / delete each field, or drop a document.

My issue appear when I began to add Data from the view :

Sometimes, the next documents added from the view are inserts in the middle of the collection so the order isn't fixed,
same things append from the mongo shell.

I found here the explanation :
https://groups.google.com/d/msg/mongodb-user/B5jU0uyGhh8/i3LacXcXKDwJ

So I have to handle the order I wan't the document are displayed by the view and have no idea how to do this :p

Someone can suggest a solution, maybe a different pattern I should use ?

Kuruwan
  • 54
  • 1
  • 8

1 Answers1

0

There is the solution for iterating over a collection Mongo and return a list of dict ordered by "index" field for each document :

def collecList(collection):
    liste, keys, value = [], [], []
    for row in collection.find().sort("index"):
        keys.append(row.keys())
        value.append(row.values())
    zipped = zip(keys, value)
    for x, y in zipped:
         liste.append(dict(zip(x, y)))
    return liste
Kuruwan
  • 54
  • 1
  • 8