Click here to Skip to main content
15,885,278 members
Home / Discussions / Database
   

Database

 
AnswerRe: SQL Server Management Studio VS SQL Server in Visual Studio 2019 Pin
Eddy Vluggen22-Nov-19 15:10
professionalEddy Vluggen22-Nov-19 15:10 
GeneralRe: SQL Server Management Studio VS SQL Server in Visual Studio 2019 Pin
Ebrahimaw25-Nov-19 16:48
Ebrahimaw25-Nov-19 16:48 
GeneralRe: SQL Server Management Studio VS SQL Server in Visual Studio 2019 Pin
Eddy Vluggen26-Nov-19 1:32
professionalEddy Vluggen26-Nov-19 1:32 
QuestionSQL Server How to add and update element in xml data in table based on match found after search using xquery Pin
Mou_kol16-Nov-19 8:20
Mou_kol16-Nov-19 8:20 
QuestionPivoting in SQL Pin
Anandkumar Prajapati12-Nov-19 23:50
professionalAnandkumar Prajapati12-Nov-19 23:50 
AnswerRe: Pivoting in SQL Pin
Richard Deeming13-Nov-19 2:17
mveRichard Deeming13-Nov-19 2:17 
GeneralRe: Pivoting in SQL Pin
#realJSOP15-Nov-19 0:18
mve#realJSOP15-Nov-19 0:18 
QuestionHow to think in MongoDB/Mongoose ?! Pin
AlexanderKolarov7-Nov-19 11:33
AlexanderKolarov7-Nov-19 11:33 
Hello, all!

I'm experienced front-end developer. Few months ago I started play with back-end technologies - nodejs, express, mongo,mongoose. I can get everything quickly. Just need some help to initiate thinking over there.


Now I develop polls application and struggle with how to structure my mongoose schemas to implement my features. Also there is implemented simple authentication.
Don't worry everything will be explained step by step.


Features:

- User can create poll on "POST /polls". Poll contains question:string, and options:[string];
- User can vote for poll on "POST /polls/:pollID/vote" as select option index;
- User can get polls on "GET /polls?...". But you receive their vote result only if you are creator or voter. And you can see always the count of all voters;
- User can bookmark poll on "POST /polls/:pollID/bookmark;
- User can get all bookmarked polls on "GET /poll/:pollID/bookmarks sorted by bookmark date;

Front-end poll object looks like;
-Always show question, options, votesCount;
-Only show vote if user is creator or voted for the poll;
-Always show if user bookmarked the poll;

My current Poll schema:
{
    question: {
        type: mongoose.Schema.Types.String,
        required: true,
    },
    options: {
        type: [{
            type: mongoose.Schema.Types.String,
            required: true,
        }]
    },
    optionsVote: {
        type: mongoose.Schema.Types.Map,
        of: mongoose.Schema.Types.Number,
    },
    createdAt: {
        type: mongoose.Schema.Types.Date,
        default: Date.now,
    },
    votes: {
        type: [{
            user: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'User',
            },
            option: mongoose.Schema.Types.Number,
        }]
    },
    votesCount: {
        type: mongoose.Schema.Types.Number,
        default: 0,
    },
    creator: {
        type: mongoose.Schema.Types.ObjectId,
        required: true,
    },
    bookmarks: {
        type: [{
            user: {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'User',
            },
            createdAt: mongoose.Schema.Types.Number,
        }]
    },
}

What I did then ...

// First added useless field in the schema I think. That helped my to limit the access(with query, not with pure js on the server) of $optionsVote field map for those that did not vote. Cannot I use just $votes field array?

// Poll Schema: 
{
   ...
    voters: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    }],
   ...
}

// Route handler "GET /polls/?category=''&order='asc'..."
const polls = await Poll.aggregate([
            {
                $project: {
                    question: true,
                    options: true,
                    createdAt: true,
                    votesCount: true,
                    optionsVote: {
                        $cond: {
                            if: {$in: [mongoose.Types.ObjectId(req.user.id), "$voters"]},
                            then: "$optionsVote",
                            else: "$$REMOVE",
                        },
                    },
                },
            }
        ]).sort({[category]: order})


So my question ...
Can be divided into:
Is the current schema right for that features?
Should I create Bookmark and Vote collections?
What queries should I implement for bookmarks and votes to be with partially restricted access ?
Can you give me direction?


I will discuss, support and edit what is needed. Thumbs Up | :thumbsup:

... And I am extremely grateful for your attention! Thank you very much! Smile | :)
Questiondatabase ER diagrime Pin
Member 1463562026-Oct-19 4:01
Member 1463562026-Oct-19 4:01 
AnswerRe: database ER diagrime Pin
OriginalGriff26-Oct-19 4:02
mveOriginalGriff26-Oct-19 4:02 
QuestionRe: database ER diagrime Pin
Member 1463562026-Oct-19 4:14
Member 1463562026-Oct-19 4:14 
AnswerRe: database ER diagrime Pin
Richard MacCutchan26-Oct-19 4:18
mveRichard MacCutchan26-Oct-19 4:18 
AnswerRe: database ER diagrime Pin
Victor Nijegorodov26-Oct-19 5:21
Victor Nijegorodov26-Oct-19 5:21 
AnswerRe: database ER diagrime Pin
Mycroft Holmes26-Oct-19 13:05
professionalMycroft Holmes26-Oct-19 13:05 
GeneralRe: database ER diagrime Pin
Richard MacCutchan26-Oct-19 21:46
mveRichard MacCutchan26-Oct-19 21:46 
QuestionFetch data Pin
Member 1463481125-Oct-19 2:34
Member 1463481125-Oct-19 2:34 
AnswerRe: Fetch data Pin
Richard Deeming25-Oct-19 2:58
mveRichard Deeming25-Oct-19 2:58 
QuestionRe: Fetch data Pin
ZurdoDev25-Oct-19 4:22
professionalZurdoDev25-Oct-19 4:22 
Questionc# MongoDB Driver, find all products that contain a single brand name, and Group Distinct all category names, then get those categories. Pin
jkirkerx22-Oct-19 10:25
professionaljkirkerx22-Oct-19 10:25 
AnswerRe: c# MongoDB Driver, find all products that contain a single brand name, and Group Distinct all category names, then get those categories. Pin
jkirkerx22-Oct-19 10:41
professionaljkirkerx22-Oct-19 10:41 
QuestionHow do you separate in pages database results? Pin
Joan M21-Oct-19 10:00
professionalJoan M21-Oct-19 10:00 
AnswerRe: How do you separate in pages database results? Pin
David Mujica23-Oct-19 2:48
David Mujica23-Oct-19 2:48 
QuestionI have a column and i need to find in which table this column exists throughout whole DB Pin
Anandkumar Prajapati8-Oct-19 19:29
professionalAnandkumar Prajapati8-Oct-19 19:29 
AnswerRe: I have a column and i need to find in which table this column exists throughout whole DB Pin
Victor Nijegorodov8-Oct-19 20:46
Victor Nijegorodov8-Oct-19 20:46 
AnswerRe: I have a column and i need to find in which table this column exists throughout whole DB Pin
Jörgen Andersson8-Oct-19 21:11
professionalJörgen Andersson8-Oct-19 21:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.