Populating field values for referred documents in aggregate call in Mongoose - node.js

I have two base schemas User and Course
//User Model
var UserSchema = new Schema({
userId: {
type: String,
default: '',
required: 'Please provide UserId.',
index : true
},
Name :{
type: String,
default: '',
trim : true
}
});
//Course Schema
var CourseSchema = new Schema({
title: {
type: String,
default: '',
required: 'Please fill course title',
trim: true,
index: true
},
description: {
type: String,
default: '',
trim: true
},
category: {
type: String,
ref: 'Category',
index: true
}
});
And a UserCourseProgress schema for storing each user's course progress.
var UserCourseProgress= new Schema({
userId: {
type: String,
ref:'User',
required: 'Please provide the User Id',
index:true
},
courseid: {
type: Schema.Types.ObjectId,
ref: 'Course',
required: 'Please provide the Course Id',
index: true
},
timespent: {
type: Number, //seconds
default: 0
},
score: {
type: Number
}
});
Now I have to aggregate results such over UserCourseProgress schema on Course level such that i show my result like this :
- {Course Name}( {No_Of_Users_taken_Course} ) [e.g. Course A (15) ]
History.aggregate([{
$group: {"_id": {"primary": "$courseid"},
"popularityCount": {$sum: 1}}},
{$sort:{"popularityCount":-1}},
{$project:{"_id":0,
"courseid":"$_id.primary",
"count":1}}
])
Now want to populate Course title from Course Schema through its reference, but after going through the list of aggregation operators(here).I am not able to figure out a way to do so.
Can you suggest any way to populate data from other collections ??

Related

How to 'join' or populate an array

Here is my very basic product schema:
const productSchema = new Schema({
productName: {
type: String,
required: true,
},
productDescription: {
type: String,
},
productPrice: {
type: Number,
},
});
module.exports = mongoose.model("Product", productSchema);
These products are listed out and a user can add a quantity for each. I am storing in an array of objects as per below. I want to join these two collections together so that I can output the qty of products selected by the user. I believe I need to use populate here but not sure how to set up the Refs and so on.
const PartySchema = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
catering: [{ id: mongoose.Types.ObjectId, qty: Number }],
created_at: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("Party", PartySchema);
I'm sharing this solution with the assumption that the catering field is the sub-document array pointing to the Product Schema:
The Product Schema is fine so it stays the same (although to keep to convention I would advice naming your schema 'Products' instead of 'Product', Mongo Naming Covention):
const productSchema = new Schema({
productName: {
type: String,
required: true,
},
productDescription: {
type: String,
},
productPrice: {
type: Number,
},
});
module.exports = mongoose.model("Products", productSchema);
And next the Party Schema would be:
const PartySchema = new Schema({
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
catering: [{
id: {
type: mongoose.Types.ObjectId,
ref: 'Products',
},
qty: {
type: Number,
}
}],
created_at: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model("Parties", PartySchema);

Mongoose schema with field of an array of other schemas

first of all, I am a beginner and currently, I am working on a social media blog type.
Now, I have my userSchema and postSchema models:
USER MODEL
const userSchema = new mongoose.Schema({
name: {
type: String,
required: [true, 'Please insert your name'],
},
email: {
type: String,
required: [true, 'Please insert your email'],
unique: true,
lowercase: true, //transform into lowercase / not validator
validate: [validator.isEmail, 'Please provide a valid email'],
},
avatar: {
type: String,
},
role: {
type: String,
enum: ['user', 'admin'],
default: 'user',
},
password: {
type: String,
required: [true, 'Please provide a password'],
minLength: 8,
select: false,
},
passwordConfirm: {
type: String,
required: [true, 'Please confirm your password'],
validate: {
validator: function (el) {
return el === this.password;
},
message: 'Passwords are not the same',
},
},
passwordChangedAt: Date,
posts: [] // ???????????????
});
POST MODEL
const postSchema = new mongoose.Schema({
title: {
type: String,
required: [true, 'A post must have a title'],
},
author: {
type: String,
required: [true, 'A post must have a title'],
},
likes: {
type: Number,
default: 10,
},
comments: {
type: [String],
},
image: {
type: String,
},
postBody: {
type: String,
required: [true, 'A post must contain a body'],
},
createdAt: {
type: Date,
default: Date.now(),
select: false,
},
});
Now I don't know if it's the best approach but I was thinking of having a field in userSchema with the type of an array of postSchema so I will have for each user their own posts created. Can I do that? If not how I can achieve that?
Should I use search params fields to filter posts by the author? I am really confused how I should approach this situation. Thank you guys
Check out this example
const UserSchema = new mongoose.Schema({
username: String,
posts: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Post'
}]
})
const PostSchema = new mongoose.Schema({
content: String,
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
})
const Post = mongoose.model('Post', PostSchema, 'posts');
const User = mongoose.model('User', UserSchema, 'users');
module.exports = { User, Post };
Credit: https://medium.com/#nicknauert/mongooses-model-populate-b844ae6d1ee7

How to better structure mongoose schemas with relationships

At the moment i have 4 models. User, profile, interests and tokens. Between user and profile there is a one to one relationship. Between User and tokens there is a one to many relationship. Between profile and interests there is also a one to many relationships, interests will be pre defined with the ability for an admin to add more later.
User
var UserSchema = new Schema({
email: {
type: String,
lowercase: true,
unique: true,
required: true
},
phone: [{countrycode: String}, {number: String}],
tokens: [{type: Schema.Types.ObjectId, ref: 'Token'}],
profile: {
type: Schema.Types.ObjectId, ref: 'Profile'
},
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
});
Profile
var ProfileSchema = new Schema({
username: {
type: String,
unique: true,
},
firstname: {
type: String
},
lastname: {
type: String
},
gender: {
type: String
},
dob: {
type: Date
},
country: {
type: String
},
city: {
type: String
},
avatar: {
type: String
},
about: {
type: String
},
interests: [{
type: Schema.Types.ObjectId,
ref: 'Interest'
}],
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
});
Token
var TokenSchema = new Schema({
name: {
type: String,
},
value: {
type: String,
},
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
});
Interests
var InterestSchema = new Schema({
name: {
type: String,
unique: true,
},
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
});
Have i set up these schemeas/relationships properly? Now if i wanted to give roles to a user would i create a new role schema?
thanks.
I think you need Relational database if you want to make relation in NoSQL db
You can't add relations in NoSQL. Only thing you can is to use schema as type of field in another schema, like
var Comments = new Schema({
title: String,
body: String,
date: Date
});
var BlogPost = new Schema({
author: ObjectId,
title: String,
body: String,
date: Date,
comments: [Comments],
meta: {
votes : Number,
favs : Number
}
});
mongoose.model('BlogPost', BlogPost);
Embedded Documents

Mongoose referencing parent in embedded document

I am fairly new to mongoose so bear with me a little. I have the following two simple schemas defined.
var ShipmentSchema = new Schema({
shipmentId: {
type: String,
default: '',
required: 'Please fill Shipment Id',
trim: true
},
dateInvoiced: {
type: Date,
default: Date.now
},
vendorInvoices:
[
{
referenceNo: {type: Schema.ObjectId,ref: 'VenderInvoice'}
}
],
});
mongoose.model('Shipment', ShipmentSchema);
var VendorInvoiceSchema = new Schema({
type: {
type: String,
default: '',
trim: true
},
referenceNo: {
type: String,
default: '',
trim: true
},
cases: {
type: Number,
required: 'Please fill in number of cases',
},
dateShipped: {
type: Date,
default: Date.now
},
invoicedProducts:
[
{
productId: {type: Schema.ObjectId,ref: 'Shoe'},
}
]
});
mongoose.model('VenderInvoice', VendorInvoiceSchema);
If I want to get a list of VendorInvoices and want to include the shipment that they belong to is there any way to do so? I know how to get a shipment and its nested list of invoices, but if I wanted to go the other direction is there a way? Or do I need to normalize in this case?
Thank you

Is it possible to have an array of alternative subdocument schemas in mongoose

Is it possible to have an array of alternative subdocument schemas in mongoose?
For instance, take this made up scenario: if i have a main schema for a person and i have a field called vehicles of type array, i want to be able to define sub document schemas, but for each vehicle type (car, motorbike, bike, bus etc etc ) :
var car = new mongoose.Schema({
make: { type: String, required: true, default: 'Ford', trim: true },
model: { type: String, trim: true },
wheels: Number,
});
var motorbike = new mongoose.Schema({
make: { type: String, required: true, default: 'Honda', trim: true },
model: { type: String, trim: true },
seats: Number,
});
var bike = new mongoose.Schema({
make: { type: String, required: true, default: 'MuddyFox', trim: true },
model: { type: String, trim: true },
lights: Number,
});
var bus = new mongoose.Schema({
make: { type: String, required: true, default: 'MAN', trim: true },
model: { type: String, trim: true },
dents: Number,
});
/* person */
var person = new mongoose.Schema({
name: { type: String, required: true, default: 'Joe Bloggs', trim: true },
timeCreated: { type: Date, required: true, default: Date.now },
vehicles: [car, motorbike, bike, bus] /* this is what i want to do */
});
db.model('person', person);
Note the person schema.
So that person can have any of those vehicles, but the data for each vehicle is still validated against it's own schema.
Thanks in advance
There is a mongoose plugin called mongoose-schema-extend, that will allow you to enable Schema Inheritance for Mongoose, and basically I think, you are looking for this functionality.
Please check the github project:
https://github.com/briankircho/mongoose-schema-extend
You can create a Vehicle schema, add it as an array to the Person schema, and extend the Vehicle for your different type of vehicles.
I couldn't find any examples of multiple embedded documents being passed as an array.
Perhaps you could rejig things like this:
var car = new mongoose.Schema({
make: { type: String, required: true, default: 'Ford', trim: true },
model: { type: String, trim: true },
wheels: Number,
});
var motorbike = new mongoose.Schema({
make: { type: String, required: true, default: 'Honda', trim: true },
model: { type: String, trim: true },
seats: Number,
});
var bike = new mongoose.Schema({
make: { type: String, required: true, default: 'MuddyFox', trim: true },
model: { type: String, trim: true },
lights: Number,
});
var bus = new mongoose.Schema({
make: { type: String, required: true, default: 'MAN', trim: true },
model: { type: String, trim: true },
dents: Number,
});
var transport = new mongoose.Schema({
car: [car],
motorbike: [motorbike],
bike: [bike],
bus: [bus]
});
/* person */
var person = new mongoose.Schema({
name: { type: String, required: true, default: 'Joe Bloggs', trim: true },
timeCreated: { type: Date, required: true, default: Date.now },
vehicles: [transport]
});
db.model('person', person);
If that doesn't work, then as described in this SO question you can use mongoose populate. This method is now more effective following the addition of deep population was added in Mongoose Deep 3.6.
So you could do soemthing like:
var personSchema = Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
There's also a mongoose plugin to aid with this functionality called mongoose-deep-populate which is very up-to-date.

Resources