I am currently facing challenges in caching friends from social media in the user's document. Initially, I attempted to clear out the existing friends cache and replace it with fresh data fetched from the social media platform. However, I encountered an issue where the embedded documents needed to be unique across all users. This led to a duplicate key error as shown below:
{"name":"MongoError","err":"E11000 duplicate key error index: test-dev.users.$friends.outsideID_1 dup key: { : \"1205314313242\" }","code":11001,"n":0,"connectionId":274,"ok":1}
It became evident that the system was unable to create a new document because a similar document already existed in another account. This prompted me to try a different approach by using OutsideFriend.find({'outsideID':{$in:friendsIDs}}) to identify and add new friends while avoiding duplicates. Unfortunately, the query did not return any documents, hinting at potential issues with centralized storage of embedded documents.
As I pondered on this dilemma, I questioned why my initial method failed if there were discrepancies in how the embedded docs were being handled. How should I proceed with handling this situation effectively? (Refer to schema below for reference)
Current Schema:
//External friend model
var OutsideFriend = new Schema({
outsideID:{type:String, index:true, unique:true}
,username:String
,location:String
,avatarURL:String
});
//User model
var User = new Schema({
avatarURL:String
,mySMID:String
//Social Media info
,smID:{type:String, index:true, unique:true}
,tok:String
,tokSec:String
,friends:[OutsideFriend]
};