Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
Maintaining auto ranking as a column in MongoDB

I am using MongoDB as my database.

I have data which contains rank and name as columns. Now a new row can be updated with a rank different from ranks already existing or can be same.

If same then the ranks of other rows must be adjusted .

Rows having lesser rank than the to be inserted one must be incremented by one and the rows which are having ranks can remain as it it.

Feature is something like number bulleted list in MS Word type of applications. Where inserting a row in between adjust the numbering of other rows below it.

Rank 1 is the highest rank.

For e.g. there are 3 rows

Name  Rank
A       1
B       2
C       3

Now i want to update a row with D as name and 2 as rank. So now after the row insert, the DB should like below

Name  Rank
A       1
B       3
C       4
D       2 

Probably using Database triggers i can achieve this by updating the other rows.

I have couple of questions

(a) Is there any other better way than using database trigger for achieving this kind of scenario ? Updating all the rows might be a time consuming job.

(b) Does MongoDB support database trigger natively ?

Best Regards,

Saurav

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No, MongoDB, does not provide triggers (yet). Also I don't think trigger is really a great way to achieve this.

So I would just like to throw some ideas, see if it makes sense.

Approach 1

Maybe instead of disturbing those many documents, you can create a collection with only one document (Let's call the collection ranking). In that document, have an array field call ranks. Since it's an array it's already maintaining a sequence.

{
 _id : "RANK",
 "ranks" : ["A","B","C"]
}

Now if you want to add D to this rank at 2nd position

db.ranking.update({_id:"RANK"},{$push : {"ranks":{$each : ["D"],$position:1}}});

it would add D to index 1 which is 2nd position considering index starts at 0.

{
 _id : "RANK",
 "ranks" : ["A","D","B","C"]
}

But there is a catch, what if you want to change C position to 1st from 4th, you need to remove it from end and put it in the beginning, I am sure both operation can't be achieved in single update (didn't dig in the options much), so we can run two queries

db.ranking.update({_id:"RANK"},{$pull : {"ranks": "C"}});
db.ranking.update({_id:"RANK"},{$push : {"ranks":{$each : ["C"],$position:0}}});

Then it would be like { _id : "RANK", "ranks" : ["C","A","D","B"] }

maintaining the rest of sequence.

Now you would probably want to store id instead of A,B,C etc. one document can be 16MB so basically this ranks array can store more than 1.3 million entries of id, if id is MongoDB ObjectId of 12 bytes each. if that is not enough, we still have option to have followup document(s) with further ranking.

Approach 2

you can also, instead of having rank as number, just have two field like followedBy and precededBy.

so your user document would look

{
 _id:"A"
 "followedBy":"B",
}
{
 _id:"B"
 "followedBy":"C",
 "precededBy":"A"
}
{
 _id:"c"
 "precededBy":"B",
}

if you want to add D at second position, then you need to change the current 2nd position and you need to insert the new One, so it would be change in only two document

{
 _id:"A"
 "followedBy":"B",
}
{
 _id:"B"
 "followedBy":"C",
 "precededBy":"D" //changed from A to D
}
{
 _id:"c"
 "precededBy":"B",
}
{
 _id:"D"
 "followedBy":"B",
 "precededBy":"A"
}

The downside of this approach is that you cannot sort in query based on the ranking until and unless you get all these in application and create a linkedlist sort of structure.

This approach just preserve the ranking with minimum DB changes.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!