I am trying to use an index to access documents in a collection using a particular index in Python 3.5 with Pymongo 3.3.0 and Mongodb 3,2. I created a field called sequence
in each document that contains a number defining the load order of the documents. I created an index on this field and gave the index the name 'sequence'
using the following command:
db.pages.create_index([('pages.sequence', pymongo.ASCENDING)], name='sequence')
The code used to load the documents is:
with MongoClient(tz_aware=True) as client:
db = Database(client, name)
for doc in db['pages'].find().hint('sequence').limit(1):
...
The document returned in doc does not contain the sequence number 1 and is not the first document loaded into Mongodb. How should I ensure that documents are returned in ascending order based on the value in the 'sequence'
field in each document?
Edit:
Sort
cannot be used as the collection is too big, roughly 16GB in size. The documentation seems to state that using an index will return data in the order of the keys in the index. If this is not so, is it not possible to use an index to define the order of retrieval of documents in a collection?