Skip to content
Advertisement

Does mongo db ListField maintain ordering?

If I add items to a ListField on a mongo db document in a specific order and then write it to the database, is it guaranteed that all subsequent reads will see the list in the same order?

e.g. write

obj = MyDocument.by_id(id)
obj.list_field = []
obj.list_field.add("a");
obj.list_field.add("b");
obj.list_field.add("c");
obj.save()

read

obj = MyDocument.by_id(id)
for x in obj.list_field:
    print(x)

# will it always print in this order?
# a
# b
# c

this is using python mongoengine ORM v0.4.2

Advertisement

Answer

Yes they do, the driver is faithful to the underlying data and mongoDb guarantees this (Do arrays stored in MongoDB keep their order?)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement