Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9088487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:55:01+00:00 2026-06-16T21:55:01+00:00

I need to store friend relationships for users in MongoDB, using Spring Data. My

  • 0

I need to store friend relationships for users in MongoDB, using Spring Data. My “schema” solution is to store the username (which is also the _id) of friends inside the user document. I’m not using DBRef because there seems to be a problem with Spring Data and DBRef on a self relationship (friends are users :p). A simple user document is something like this:

{
"_id" : "user1",
"email" : "user1@test.com",
"friendRequests" : {
    "user4" : 0
},
"friends" : ["user2", "user3" ],
"password" : "$2a$10$9iJWLZjBSu3rq19wh7KTduNXIVcXozsNVjwVogO9eoz0uXO52Z2NC"
}

I think this model in good enough. But when someone accept a friend request, I have to update both users’ documents and the operation is not atomic. There could be some case where only one of the two gets updated. It’s not critical data, but still would be nice to have a solution for this. Am I overthinking this? I found this document on 2 phase commit http://cookbook.mongodb.org/patterns/perform-two-phase-commits/ but it seems too much for this situation, even though it’s pretty easy to implement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:55:02+00:00Added an answer on June 16, 2026 at 9:55 pm

    It’s hard to answer these type of questions without knowing how your application will be using the data. Remember, in MongoDB there are many different ways to represent basically the same data/schema, so the way that will work best with your application is the one that fits your application’s needs.

    Some questions to ask yourself:
    Do you know how many friends on average each user will have?
    How will you be displaying/querying/updating user’s friends?
    Which of the above operations need to be performant and absolutely up-to-date and which can take longer, or be done in batch mode later?

    There are trade-offs to each possible schema for your data. Continually adding usernames into an array of friends of each user means the documents will continually grow (this is sub-optimal for performant update as when the document exceeds its allocated space it needs to be moved the next time it’s updated). To offset that, you have to consider what you gain when you store an array of friends this way. Does it enable you to make only a single read when fetching all information about the user? Or do you still need to make another read (maybe querying the users collection again for additional information about each user which is in the friends array?

    You are already thinking about atomicity of updates which help keep the data consistent, but as you correctly note, this is something that your application can handle (or you can have a background job which runs and detects any “part-way” friendship updates and cleans them up).

    You should also consider how you will need to index the collections – if you need many indexes on a single collection to satisfy the SLA for your queries, then your updates/inserts will necessarily get slower (since many more indexes will need to be updated). That may be okay but only you can make the decision about the trade-off.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using MySQL for basic storage of system/user data. I need to store/search messages (adding
Friends i need to store more than 100 mb data which come in zip
I need store just 10 arrays in my app, which I can change from
I've created a web enabled database using SQL Server 2005 to store the data
I'm thinking of a mid / large scale project, which will need to store
I need to parse an XML file and store the data to sql server
I need to build a system to store the following data: 10,000 stocks For
I am working with twitter data and need to store uid and friendId pair
I have id values for products that I need store. Right now they are
I need to store a very large amount of instances of my class, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.