how reuse a like model in nodejs - node.js

I have 2 models named "posts" and "status" and want to implement likes in them. first of all, I want the like to be able to record data like timestamps and other stuff depending on how it grows, which is why I made "like" be a model of its own.
The issue is since "posts" and "status" two models of their own are going to have "like" functionality.
Is there a way I could reuse the "like" model, instead of creating a separate "like" model for "posts" and "status", or how would you personally implement something like this?
Below is the post model
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true
},
description: {
type: String,
required: true,
trim: true
},
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
}
}, {
timestamps: true
})
below is the status model
const statusSchema = new mongoose.Schema({
Body: {
type: String,
required: true,
trim: true
},
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
tags: [{
type: mongoose.Schema.Types.ObjectId,
required: True,
ref: 'User'
}]
}, {
timestamps: true
})
here is the like model which I would like users to be able to like both posts by users and statuses, while still able to retain information like the time it was liked and other information depending on the growth and need
const likeSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
likedObject: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'Posts'
}
}, {
timestamps: true
})
Is there a way I could reuse the "like" model, instead of creating a separate "like" model for "posts" and "status" to capture the users and the time that they liked other user's statuses and posts?

Making Like Model was over complicating the entire thing so you could have something like this in your postSchema.
const postSchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true
},
description: {
type: String,
required: true,
trim: true
},
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
timeStamps:true,
//Adding the like property in each post but setting it to a default of 0
likes:{type:Number, default:0}
})
Now the key is identifying the post when like button is smashed
So what you could do on the client side is making sure that when like button is clicked you have an ID of the post to the function that your calling and, then pass that ID back to the server and you can have a logic like this below on your server and you would be able to add like functionality..
Server logic for adding like functionality
router.post("/api/likes/:id", async (request, response) => {
const post_id = request.params.id;
const post = await postModel.findOne({ _id: post_id });
post.likes += 1;
const updateDocument = await postModel.findOneAndUpdate(
{ _id: post_id },
post,
{
new: true,
}
);
return response.status(201).json({ msg: "Liked post" });
});
So the idea is two always be updating that specific document

Related

Best way to structure mongoDB schemas for a Trello-like app using mongoose?

Right now i'm thinking of doing it like this:
Board:
const BoardSchema = new Schema({
title: {
type: String,
required: true,
},
admins: [
{
type: ObjectId,
ref: 'User',
required: true,
}
],
members: [
{
type: ObjectId,
ref: 'User',
required: true,
}
],
}, {
timestamps: true,
});
List:
const listSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
order: {
type: Number,
required: true,
},
boardId: {
type: ObjectId,
required: true,
ref: 'Board',
},
});
Card:
const cardSchema = new Schema({
text: {
type: String,
required: true,
},
members: {
type: ObjectId,
ref: 'User',
required: true,
},
listId: {
type: ObjectId,
ref: 'User',
required: true,
},
boardId: {
type: ObjectId,
ref: 'Board',
required: true,
},
order: {
type: Number,
required: true,
},
});
I'm new to mongoDB/noSQL and a database noob in general, and structuring a database seems to not be a very strict science, as everyone seems to do it differently. From what i've seen the rule of thumb is to keep as much data in one collection as possible, basically the opposite of traditional databases, but not always? So should i instead save everything in the Board collection?
Going forward with this project i want to try to replicate how one would do in a real scenario to learn as much as possible. And i've been googling around to try and find the schema structure for the real trello app but i cannot find it. All i know is that they do use mongoDB and they have a separate Collection for Cards because with the amount of data they handle it wouldn't work otherwise. So i assume they have a separate Collection for Lists (and Boards) aswell.
So my question is if these Schemas would work? And what would be the best way to go about getting all the data (when a user clicks on a board) using the boardId and combining it into a List of Cards?

mongodb aggregate $lookup vs find and populate

I have a Video Schema like this:
const VideoSchema = new mongoose.Schema({
caption: {
type: String,
trim: true,
maxlength: 512,
required: true,
},
owner: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
// some more fields
comments: [{
type: mongoose.Schema.ObjectId,
ref: 'Comment',
}],
commentsCount: {
type: Number,
required: true,
default: 0,
},
}, { timestamps: true });
and a simple Comment schema like this:
const CommentSchema = new mongoose.Schema({
text: {
type: String,
required: true,
maxLength: 512,
},
owner: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
videoId: {
type: mongoose.Schema.ObjectId,
ref: 'Video',
required: true,
index: true,
},
}, { timestamps: true });
and with schemas like this I'm able to perform any kind of find query on my Video collection and populate it with its comments:
Video.find({ owner: someUserId }).populate({ path: 'comments' });
My question is how necessary is it to keep comment ids inside video collection? given that I have indexed the videoId field in my Comment schema, how bad it would be (speaking of performance) to get rid of these comment ids and the count of them and use aggregation $lookup to find a video's comments like this:
Video.aggregate([
{
$match: {
owner: someUserId,
},
},
{
$lookup: {
from: 'comments',
localField: '_id',
foreignField: 'videoId',
as: 'comments',
}
}
])
How different are these in terms of performance?
Well there is no way the $lookup would be faster than having the list of the comment ids on the actual video object. I mean you have to do a whole other request to mongo to get them now. So performance wise obviously the lookup would add time. That is assuming you are not using mongoose populate to "convert" those comment ids into the referenced objects.
If you are then removing the comments from the video (as well as the actual count prop) and doing the lookup is the way to go. Since you are matching right away in your arg and then doing a simple lookup I do not see how this would be a bottleneck for you. Also you can optimize/change/tune your aggregation vie explain etc.
You video schema would be pretty clean that way:
const VideoSchema = new mongoose.Schema({
caption: {
type: String,
trim: true,
maxlength: 512,
required: true,
},
owner: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
// some more fields
}, { timestamps: true });

Dont know how to populate Mongoose Query

I have a problem with a mongoose population and I don't know what I should do.
I got two schemas:
var userSchema = new userSchema({
username: { type: String, required: true, unique: true },
password: { type: String, required: true },
mods: [{ type: mongoose.Schema.Types.ObjectId, ref: 'users'}]
});
var dataSchema = mongoose.Schema({
title: { type: String, required: true, unique: true },
description: { type: String, required: true },
owner: {type: mongoose.Schema.Types.ObjectId, required: true}
});
So one user can have several data packages.
Some users are moderated by other users.
Whats the query for a moderator, that all his own data packages and the ones of the users he is moderating are listed?
You see that I have a SQL background and there's definitely another way to do it with MongoDB.
Thanks for your help!
I'm not clear understand what queries do you need but first you need set ref property in 'owner' field in dataSchema. As about population it's look like this:
//if you use callback
users.find({/*your query*/}).populate('mods')
.exec((err, result)=>{/*your code*/});
//if you use promise
users.find({/*your query*/}).populate('mods').exec()
.then(result=>{/*your code*/})
.catch(err=>{throw err});

Find all the documents that has same subdocuments mongoose

I have postSchema which references the tagsSchema.
var tagsSchem = new Schema({
name: {
type: String,
required: true
}
}, {
timestamps: true
});
// create a schema
var postsSchema = new Schema({
title: {
type: String,
required: true,
unique: true
},
mainImage: {
type: String
},
category: {
type: String,
required: true
},
body: {
type: String,
required: true
},
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
tags: [tagsSchem]
}, {
timestamps: true
});
One post can contain any no. of tags. So if a post has 3 tags then I want to get all the posts with those 3 tags without querying it multiple times. Is it possible?
When you perform find, you can use the $in option to find values that are in your array. For example:
posts.find({tags:{$in:{["tag1","tag2","tag3"]}}, function(err,data) {
... //Your code here
}
This will take all the posts that contains one of the three tags. It's important you have to pass an array in the $in option. This should work.

Get info from 2 separate Mongo documents in one mongoose query

Im using MongoDb, and I have a workspace schema with mongoose (v4.0.1):
var Workspace = new mongoose.Schema({
name: {
type: String,
required: true
},
userId: {
type: String,
required: true
},
createdOn: {
type: Date,
"default": Date.now
}
});
And a user schema:
var User = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true
},
organisation: {
type: String,
required: true
},
location: {
type: String,
required: true
},
verifyString: {
type: String
},
verified: {
type: Boolean,
default: false
},
password: {
type: String,
required: true
},
createdOn: {
type: Date,
"default": Date.now
},
isAdmin: {
type: Boolean,
default: false
}
});
So the Workspace userId is the ObjectID from the User document.
When Im logged in as an adminstrator, I want to get all workspaces, as well as the email of the user that owns the workspace.
What Im doing is getting very messy:
Workspace.find({}).exec.then(function(workspaceObects){
var userPromise = workspaceObects.map(function(workspaceObect){
// get the user model with workspaceObect.userId here
});
// somehow combine workspaceObjects and users
});
The above doesnt work and gets extremely messy. Basically I have to loop through the workspaceObjects and go retrieve the user object from the workspace userId. But because its all promises and it becomes very complex and easy to make a mistake.
Is there a much simpler way to do this? In SQL it would require one simple join. Is my schema wrong? Can I get all workspaces and their user owners email in one Mongoose query?
var Workspace = new mongoose.Schema({
userId: {
type: String,
required: true,
ref: 'User' //add this to your schema
}
});
Workspace.find().populate('userId').exec( (err, res) => {
//you will have res with all user fields
});
http://mongoosejs.com/docs/populate.html
Mongo don't have joins but mongoose provides a very powerfull tool to help you with you have to change the model a little bit and use populate:
Mongoose population
You have to make a few changes to your models and get the info of the user model inside your workspace model.
Hope it helps

Resources