Queries change/alter the order/arrangement of the documents in a collection.
Mongoose ver ^5.3.10
Node ver. 11.1.0
Mongod version: 3.6.8 (MMAPv1)
Type: Bug/issue
Behavior: While running queries like db.findOneAndUpdate & db.updateOne, it changes the order of the documents in the db/collection, after finishing the queries.
Please refer to this paste here for a better view.
From a collection like this:
please note the id and not _id
[ {
"_id": {
"$oid": "5be5772763b2845ef4682afd"
},
"id": "1",
"name": "Neo",
"email": "[email protected]",
"age": 37,
"gender": "Male",
"meds": [],
"__v": 0
},
{
"_id": {
"$oid": "5be5772763b2845ef4682afd"
},
"id": "2",
"name": "Neo",
"email": "[email protected]",
"age": 37,
"gender": "Male",
"meds": [],
"__v": 0
},
{
"_id": {
"$oid": "5be5772763b2845ef4682afd"
},
"id": "3",
"name": "Neo",
"email": "[email protected]",
"age": 37,
"gender": "Male",
"meds": [],
"__v": 0
}
]
To:
[ {
"_id": {
"$oid": "5be5772763b2845ef4682afd"
},
"id": "3",
"name": "Neo",
"email": "[email protected]",
"age": 37,
"gender": "Male",
"meds": [],
"__v": 0
},
{
"_id": {
"$oid": "5be5772763b2845ef4682afd"
},
"id": "1",
"name": "Neo",
"email": "[email protected]",
"age": 37,
"gender": "Male",
"meds": [],
"__v": 0
},
{
"_id": {
"$oid": "5be5772763b2845ef4682afd"
},
"id": "2",
"name": "Neo",
"email": "[email protected]",
"age": 37,
"gender": "Male",
"meds": [],
"__v": 0
}
]
Please give more details on what order does change. I can't really see something changing in order within a document from what you provided :/
@saifali96 I agree with @mhombach , I don't really see any difference.
@mhombach sorry but MD trimmed the indentation. Please refer to this paste here for a better view.
@vkarpov15 basically what I want to say is that I have a collection of users, which upon creation/first-initialization were in an order defined by their creation date, of course. eg, [ {user1}, {user2}, {user3}, … ]
but after I run queries to update/modify them via Mongoose (read the OP), the order/arrangement of the collection of users is altered, from the previous order to this [ {user2}, {user3}, {user1}, … ] (if I modify user1), basically whichever document I modify in the collection, it gets bumped up to the end of the collection.
I tried to compare the values before and after the queries, only the values I mentioned in the query are changed and the $oid keys retain their original value and are not altered or deleted.
This is a strange behavior as I'm not deleting the objects and reinitializing them, just modifying/updating. So the objects should retain their position/index/order in the collection, right?
I hope I made the point clearer, you can also refer to the pastebin above to see the data.
p.s. I'm using the same object in the example, so it might be tricky to understand, but you can refer to id key to understand the changes in positioning.
As you already wrote, it's a bit tricky to see 100% of the case because you are using the same example-data on all 3 documents :/ But i understand what you are saying. The thing is though: If you don't define an order in your query (see for reference this and this ) you can NOT assume ANY order of the returned documents. This applies to MongoDB as it applies to MySQL, MSSQL and mostly every other database language. So... if you want consistent order, just place the sort-parameter in your query. Mongoose/MongoDB can not know for what key and what order you want your return-data sorted without you telling it ;)
@mhombach I'm glad that you understand the issue.
I'm sorry for the confusion because of the same dataset. Another mistake that I made, is that I didn't mention that the data you see above is not return by the query, but instead how the data is actually stored in the database. I'm using mLab and the data you see is the data from the explorer.
In short, although I might be relatively new to MongoDB, I know this much that I need to send a sort parameter if I wanted the returned data to by sorted by some key/index. But this is not the case, the data stored in the db (i.e. the object/document within the collection) jumps to the end of the collection after changes are made to it's field.
It's an unexpected behavior, would you agree?
@saifali96 aaaah, ok i now got your problem :) You see, there is no visible "order" on ...like the harddisk. Windows may defragmentate and move files to other sectors and so... A database is an abstraction, so you don't need to think about where the data actually is stored. Also, things like "sharding" (just google it if interested) in mongoose will split the data up to multiple computers/instances. What order you think the data is then stored? Correct: in no order at all :) You may think that the data is stored in some "raw" order because the graphical interface shows them, but the magic happens behind the curtain: Every database-explorer internally querries for the data and this query by default has no "order" parameter (or not the one order you actuallly want in your case). If you have 3 billion documents stored and access the mLab-explorer, you will NOT see 3 billion documents, right? Because in that default query there is a "limit" parameter for not pulling gigabytes of data. So... TLDR: The explorer uses a normal query for all its actions, even showing "the dataset". Things like sharding, splitting up the data, caching and much more things make it, in addition, impossible to have a "raw order" of elements. I hope that cleared things up a little bit for you :)
@mhombach glad you understood the problem now.
It is probably a sorting issue. Thanks, @vkarpov15 you can close the issue.
@saifali96 sorry to comment again, but this is NOT „probably a sorting issue“. This is no „issue“ at all. When you look at the documents in your view, then you are basically telling the database „give me some random documents, i dont care“. When you dont specify an order in which you would like to get the returned entries, you cannot expect them magically be sorted the way you want. If you dont specify an order, you have to assume that everytime you view your data, the shown order is pure random and decoded by the internal driver and at will. Sorry to be bery detailed about this but this is fundamental basic in data-management. And it is very important that you and everyone else reading this here, in weeks/months/years from now, understands that. You cannot tell a storage to give you data in the „original“ or „right“ or „raw“ order because those orders dont exist. EVERY time you request data from a storage there is an „order by xyz ascending“. Either you define it yourself or the storage will define the order itself. But if you let the storage decide the order, you can not expect consistent entries for each time you fire up the same query. Your asked question has been answered and it was a misunderstanding of how database-queries work and had nothing to do with mongoose itself. But i am glad i had the opportunity to clarify this and make it easier for you and others in the future to work with storage :) And yes, this here can be close IMO ;)
@saifali96 @mhombach is right, unless you explicitly sort() MongoDB doesn't guarantee the order of the documents. By default, MongoDB will use so-called "natural order".
Also, something is whacky with your data, I don't understand how you managed to store 3 documents with the same _id 5be5772763b2845ef4682afd :confused:
Either way, closing :+1: