[SERVER-53897] How to implement list ordering across documents? Created: 20/Jan/21 Updated: 22/Jan/21 Resolved: 21/Jan/21 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Question | Priority: | Major - P3 |
| Reporter: | Johnny Shields | Assignee: | Eric Sedor |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Participants: |
| Description |
|
Many applications, e.g. todo lists, need to maintain a user-defined order of items. Suppose I have the following collections/documents: users -> { _id, name } I'd like to have todos look something like: { user_id: abc..., position: 0, text: "Do laundry" } { user_id: abc..., position: 1, text: "Clean room" } { user_id: abc..., position: 2, text: "Brush teeth" } { user_id: def..., position: 0, text: "Walk dog" } { user_id: def..., position: 1, text: "Buy dog toy" }For each user ID, the positions should always be 0, 1, 2... N with no duplicates and no gaps. Moreover, the users should be able to re-order their list, e.g. something like this UI, and maintain integrity of the list:
The problem is further explored here: https://begriffs.com/posts/2018-03-20-user-defined-order.html I've implemented this in my Ruby application using the Mongoid Orderable gem: https://github.com/mongoid/mongoid_orderable. However, when processing many re-order actions with concurrent requests, my lists always get out of sync (position duplicates or gaps occur, e.g. positions become 0, 0, 1, 1, 1, 3, 4, 6, 6, 8... etc.) I am experimenting using transactions with mixed results. I'd like to hear if there is a recommended/canonical way to do this in MongoDB? |
| Comments |
| Comment by Eric Sedor [ 21/Jan/21 ] |
|
Thanks shields@tablecheck.com! I'm going to close this ticket but we can re-open it if necessary in the future. Be well! |
| Comment by Johnny Shields [ 21/Jan/21 ] |
|
OK, thanks, was unaware of community forums. Will see what they say. I do thing this use case reveals a feature gap in terms of what SQL databases can support vs. MongoDB. |
| Comment by Eric Sedor [ 20/Jan/21 ] |
|
The SERVER project is for bugs and feature suggestions for the MongoDB server. For this question we'd like to encourage you to start by asking our community for help by posting on the MongoDB Developer Community Forums. If the discussion there leads you to suspect a bug in the MongoDB server, or to identify a specific server-side feature that would aid this use-case, then we'd want to discuss here in the SERVER project. Thanks, |