1

I was wondering if there is a certain "order" of some sort in which MongoDB documents are returned when querying like this:

collection.find()

Is it always the same, given that collection does not change? Does MongoDB have some kind of order? There is an empty query, so it will just retrieve any document. I'm asking because this is for classification. I want to retrieve a bunch of documents to train a model on. The test set cannot include documents from that same set, so I do this:

trainset = collection.find().limit(train_set_size)
testset = collection.find().skip(train_set_size).limit(test_set_size)

So both sets will have absolutely no overlap.

Any thoughts?

Thanks!

lennyklb
  • 1,307
  • 2
  • 15
  • 32

2 Answers2

1

I think collection.find() return based on id. In mongodb ObjectId is based on time.So it should be LIFO(Last in First Out).So sorting is based on time.

In your set, there should be no overlap

itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
0

Unless you specify an specific order, you can't rely on the order of the returned results. In general the results are returned in the order they are found, which may coincide with insertion order or the order of the index(es) used.

Alex
  • 21,273
  • 10
  • 61
  • 73