I have a collection with documents that form a linked list each document has a field parentID that point to the parent document id, the problem is I want to get a sorted array based on this list the first element has parentID equal to null and the next element in the array points to the previous one, can someone guide me if this is possible to do in one query without performing multiple queries
Hi @Ay.Be,
I’m afraid it doesn’t sound like a great idea. Handling a linked list in memory is easy, but it gets complicated in a database, especially when you have to make changes to the links. Maybe you can tell us why you want to do it this way, and we can check if there’s a better option.
Hi @Jack_Yang1
I have a product collection that I want a user to be able to sort. I thought of giving each product a rank to help with sorting but it gets complicated when they try to remove a product or change a product rank because I have to change all the products ranks that comes after it (1,2,3,4) if I want to insert a document between rank 1 and rank 2 the documents 2,3,4 will change to 3,4,5 , and so linked list might be to solve the issue here for example when I remove a document I only need to change the parentID of the next document to link to the document before the one I’d like to delete, or if I’m inserting a document I’ll just update the parentID of 2 documents. I hope you understood.
take a look at $graphLookup, it might help