Skip to content

MongoDB generated ObjectID for arrays is descending instead of ascending #8514

@butlimous

Description

@butlimous

Do you want to request a feature or report a bug?

  • Report a bug or make sure it's designed like this

What is the current behavior?

I have the following document in a collection in my MongoDB database:

"_id":"5e2088563824e72ea5ee7095",
"image":"xxx",
"questions":[ 
    {
       "_id":"5e2088563824e72ea5ee70c3",
       "text":"test test test",
    },
    {
       "_id":"5e2088563824e72ea5ee70be",
       "text":"what is your age 1?",
    },
    {
       "_id":"5e2088563824e72ea5ee70b9",
       "text":"what is your gender 1?",
    },
    {
       "_id":"5e2088563824e72ea5ee70b4",
       "text":"what is your blood type 1?",
    },
    {
       "_id":"5e2088563824e72ea5ee70af",
       "text":"what is your blood type 1?",
    }
],
"__v":0

Without doing any type of sorting on the questions array, the document would always return like above which is the natural ascending order of the data entered.

What is strange is that the generated IDs are in reversed order as you can see:

key 0 => 5e2088563824e72ea5ee70c3
key 1 => 5e2088563824e72ea5ee70be
key 2 => 5e2088563824e72ea5ee70b9
key 3 => 5e2088563824e72ea5ee70b4
key 4 => 5e2088563824e72ea5ee70af

What is the expected behavior?

I expected the generated ID to match with the key of the array (ascending) so, it should be like:

key 0 => 5e2088563824e72ea5ee7096
key 1 => 5e2088563824e72ea5ee709b
key 2 => 5e2088563824e72ea5ee70a0
key 3 => 5e2088563824e72ea5ee70a5
key 4 => 5e2088563824e72ea5ee70aa

Why does Mongoose give the first element in the array the highest ObjectID and the last element the lowest ObjectID?

However, behaviour of the generated document IDs is normal like the auto-increment of MySQL (ascending). It's just the nested arrays that have the reversed order.

Is this a bug or is it the normal behavior? Why is it designed like this if this is normal?

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions