BlackHoleGalaxy
BlackHoleGalaxy

Reputation: 9662

MongoDb Asp.Core define and use key (not value) in a relationship

Context

In our app, we permit to our users to upload files, which will be placed in a queue for review and approbation.

User's file ID will be stored as a "sub-list" (I don't know the name) in the user specific Mongo Database.

Question summary

My question is all about defining a file Object ID as a key in my user database and the getting this user's uploaded file ID to retrieve the current file state value.

Our model

public class User
{
    [BsonId]
    public string Id { get; set; }
    public string email { get; set; } = string.Empty;
    public bool hasAcceptedTerms { get; set; } = false;
    public IList<UserFile> SubmittedFiles { get; set; }
}
public class UserFile
{
    public string fileID { get; set; } // Correspond to the Object Id of a file
    public bool hasFile { get; set; }
}
public class File
{
    [BsonId]
    public string Id { get; set; }
    public string fileServerIdentifier { get; set; } = string.Empty;
    public string filename { get; set; } = string.Empty;
    public string user { get; set; } = string.Empty; //Corresponds to the user object Id
    public int state { get; set; } = 0;
}

Mongo JSON database example

{
  "files" : {
    "5349b4ddd2781d08c09890f3" : {
      "fileServerIdentifier" : "7e1fed29-c3b2-4d7e-9e00-bbff38aecb51",
      "filename" : "document.pdf",
      "user" : "507f1f77bcf86cd799439011",
      "state": 0
    }
  },
  "users" : {
    "507f1f77bcf86cd799439011" : {
      "email" : "[email protected]",
      "hasAcceptedTerms" : true,
      "submitted-files" : {
        "5349b4ddd2781d08c09890f3" : true
      }
    }
  }
}

My question

  1. First of all, is my link between User and File correct: I mean the public IList<UserFile> SubmittedFiles { get; set; }?

  2. How can I add, in an existing users collection (user filtered by Id), a fileID as a key with value true.

  3. And as a matter of consequence how to retrieve recursively all the fileID which are keys and not values so I can use them to retrieve recursively the corresponding state in File database?

Upvotes: 1

Views: 1387

Answers (1)

Maksim Simkin
Maksim Simkin

Reputation: 9679

1) Your JSON is defenetly wrong. From example, how could look single user in "users" collection, take attention, SubmittedFiles is an array:

{
   "_id":"5349b4ddd2781d08c09890f3",
   "email":"[email protected]",
   "hasAcceptedTerms":true,
   "SubmittedFiles":[
      {
         "fileID":"5349b4ddd2781d08c09890f3",
         "hasFile":true
      }
   ]
}

2) It's possible to change just this one value of the array on server, but i am not sure, that i could write this mongo query. I suggest to get User from collection, change value and write user back:

var userLoaded = collection.Find(x=>x.Id =="586f92559afc29759076efd8").Single();
userLoaded.SubmittedFiles.First(x=>x.fileID=="5349b4ddd2781d08c09890f3").hasFile = true;
collection.ReplaceOne(u=>u.Id==userLoaded.Id,  userLoaded);

3) If you want get all fileIds, you could for example use this query:

collection.Find(x=>true)
           .Project(t=>t.SubmittedFiles.Select(f=>f.fileID))
           .ToEnumerable()
           .SelectMany(s=>s)

Upvotes: 1

Related Questions