0

I have a localhost mongodb where collection behavior is proper. But when I try to test this mongorestored collection within a docker container I get in trouble.

All my documents always get inserted into the middle of collection. Example is below:

db.posts.find().pretty()
{
    "_id" : ObjectId("595cc2a803297da443e36a1a"),
    "name" : "First post",
    "__v" : 0
}
{
    "_id" : ObjectId("595cc31e03297da443e36a1c"),
    "name" : "Second post",
    "__v" : 0
}
db.posts.insert({name: "Third post"})
WriteResult({ "nInserted" : 1 })
db.posts.insert({name: "Fourth post})
WriteResult({ "nInserted" : 1 })
db.posts.find().pretty()
{
    "_id" : ObjectId("595cc2a803297da443e36a1a"),
    "name" : "First post",
    "__v" : 0
}
{
    "_id" : ObjectId("5964c37ba4e0e8004c4c20d2"),
    "name" : "Third post",
    "__v" : 0
}
{
    "_id" : ObjectId("5964c41fd1302fc68fac6ee0"),
    "name" : "Fourth post",
    "__v" : 0
}
{
    "_id" : ObjectId("595cc31e03297da443e36a1c"),
    "name" : "Second post",
    "__v" : 0
}
Vadim Shvetsov
  • 2,126
  • 2
  • 20
  • 28
  • this doesn't make sense as mongodb does not garenty any order for saved documents, see [this question](https://stackoverflow.com/questions/11599069/how-does-mongodb-sort-records-when-no-sort-order-is-specified) for details. You should add a timestamp / auto-incremental field / ... and sort on it to keep the order – felix Jul 11 '17 at 13:10
  • @felix thanks. Isn't it sorted with id in this case? – Vadim Shvetsov Jul 11 '17 at 13:24

1 Answers1

0

As felix mentioned behavior of default sorting isn't obvious but after I changed docker container main image from MongoDB 3.0.15 to 3.4 the issue was gone and last inserted post was appended to collection as expected.

Vadim Shvetsov
  • 2,126
  • 2
  • 20
  • 28