Click here to Skip to main content
14,364,335 members
   

Database

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder16-Jul-09 4:10
adminChris Maunder16-Jul-09 4:10 
PinnedHow to get an answer to your question PinPopular
Chris Maunder10-Nov-05 17:30
adminChris Maunder10-Nov-05 17:30 
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 9:20
memberMou_kol16-Nov-19 9:20 
QuestionPivoting in SQL Pin
Anandkumar Prajapati13-Nov-19 0:50
professionalAnandkumar Prajapati13-Nov-19 0:50 
AnswerRe: Pivoting in SQL Pin
Richard Deeming13-Nov-19 3:17
mveRichard Deeming13-Nov-19 3:17 
GeneralRe: Pivoting in SQL Pin
#realJSOP15-Nov-19 1:18
mve#realJSOP15-Nov-19 1:18 
QuestionHow to think in MongoDB/Mongoose ?! Pin
AlexanderKolarov7-Nov-19 12:33
memberAlexanderKolarov7-Nov-19 12: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 | :)
QuestionMessage Closed Pin
26-Oct-19 11:08
memberMember 1463562026-Oct-19 11:08 
QuestionMessage Closed Pin
26-Oct-19 11:08
memberMember 1463562026-Oct-19 11:08 
Questiondatabase ER diagrime Pin
Member 1463562026-Oct-19 5:01
memberMember 1463562026-Oct-19 5:01 
AnswerRe: database ER diagrime Pin
OriginalGriff26-Oct-19 5:02
protectorOriginalGriff26-Oct-19 5:02 
QuestionRe: database ER diagrime Pin
Member 1463562026-Oct-19 5:14
memberMember 1463562026-Oct-19 5:14 
AnswerRe: database ER diagrime Pin
Richard MacCutchan26-Oct-19 5:18
protectorRichard MacCutchan26-Oct-19 5:18 
AnswerRe: database ER diagrime Pin
Victor Nijegorodov26-Oct-19 6:21
memberVictor Nijegorodov26-Oct-19 6:21 
AnswerRe: database ER diagrime Pin
Mycroft Holmes26-Oct-19 14:05
memberMycroft Holmes26-Oct-19 14:05 
GeneralRe: database ER diagrime Pin
Richard MacCutchan26-Oct-19 22:46
protectorRichard MacCutchan26-Oct-19 22:46 
QuestionFetch data Pin
Member 1463481125-Oct-19 3:34
memberMember 1463481125-Oct-19 3:34 
AnswerRe: Fetch data Pin
Richard Deeming25-Oct-19 3:58
mveRichard Deeming25-Oct-19 3:58 
QuestionRe: Fetch data Pin
ZurdoDev25-Oct-19 5:22
professionalZurdoDev25-Oct-19 5: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 11:25
professionaljkirkerx22-Oct-19 11: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 11:41
professionaljkirkerx22-Oct-19 11:41 
QuestionHow do you separate in pages database results? Pin
Joan M21-Oct-19 11:00
professionalJoan M21-Oct-19 11:00 
AnswerRe: How do you separate in pages database results? Pin
David Mujica23-Oct-19 3:48
memberDavid Mujica23-Oct-19 3:48 
QuestionI have a column and i need to find in which table this column exists throughout whole DB Pin
Anandkumar Prajapati8-Oct-19 20:29
professionalAnandkumar Prajapati8-Oct-19 20: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 21:46
memberVictor Nijegorodov8-Oct-19 21:46 

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.