mongoose - What's the optimal MongoDB architecture for finding documents to which no pointers exist in another collection? -
use case
with a science fair, judges who will enter the platform, and each one Each project per score
The problem that we are trying to solve
A judge wants to visit a URL, say http://www.judge.com/next < / Code> He continually provides the next project for which he has not created any score so far, and inform him (because all the projects have scores created by that judge) their judicial fees have been completed. Code: [[_id name: string]] Project: [[_id name: string]] Score: [{ _id type: am judge: _id project: _id}]
use sql, idiot
Obviously. But this is the question, What would we do if we wanted to make it in Mongodi?
One shot in Don
two collections, judge and project .
Judge: [[_id name: string score: type [[_id: enum project: _id}]}] projects: [{_id name: string}]
So, what is the correct architecture?
Usually there is no authentic "correct" answer for schema design in mangodibi; The best way depends on your use case and data.
Below are some ideas for using the mango
shell and keeping some warnings in mind.
If you do not get the result of any final questions, you know that the judicial duties of that person have been fulfilled. If you want to skip the effort to make this comparison once the decision is complete, you can always & amp; Check a boolean value on the judge (i.e. doneJudging
).
Spot the difference ( A shot in the dark )
-
Considering sample scores:
db.scores.insert ({{Judge: 123, Project: 1, Score: 5}, {Judge: 123, Project (Score: 9,
First of all, find all those projects A judge has rated:
var rated = db.scores.distinct ('project', {judge: 123})
Then find projects that a judge has evaluated using not :
var unrated = db example (For example: {$ nin: rated}}) (<">
> Example {"_id": 6, "name": "Project 6"}
{"_id": 4, "name": "project 4"}}
This approach requires two questions to calculate the end result, but it also allows you to get a list of all the unreserved projects for a judge.You do not need to update judges as new projects;
-
Prior to the recognizable round, begin assigning your
score
array to each judge; Project combination:db.judges.insert ({_id: 123, name: "Bobby Tables", Score: [{Project: 1, Score: 5}, {Project: 2, score : {Project: 6},}}
-
You can update or add the
score
array after making a decision, but a project will appear as code without Thatjudges
documentation. -
Use one to find the "next" unread project for a given judge. The
$ elemMatch
projection will return only the first matching element per array.db.judges.findOne (// query {_id: 123}, // Projection {_id: 0, score: {$ elemMatch: {score: null}}}
-
Example result ("
_id
to rate 123"):{"scores" : [{"Project": 4}]}
-
This approach makes it easy for a judge to get the "next project", but if you're all unprotected If you want to get projects, you will have to get all the projects and in your application code,
-
Since each judge has a list of his own unreserved projects, you can use this approach to provide a subset of projects for the judges.
-
If you add (or remove) projects, you must update all the judge documents.
Comments
Post a Comment