Organizers, Products collections, I need to join products table with posted user id and organizers collections and purchased userid with users and organizers again. I can join products with users but not able to join users with organizer.
Products.find({})
.populate({
path: 'intresteduserIds.userId',
// Get friends of friends - populate the 'friends' array for every friend
populate: { path: 'intresteduserIds.user_organizationid' } /* How to join with organizer collection which have organizer id*/
})
.populate('product_postedby');
Products schema:
const ProductsObj = new Schema({
product_title: String,
product_category: String,
product_startDate: Date,
product_startTime: String,
product_endDate: Date,
product_endTime: String,
product_isPrivateEvent: Boolean,
product_desc: String,
product_postedby: { type: mongoose.Schema.ObjectId, ref: 'User' },
intresteduserIds: [
{
userId: {
type: mongoose.Schema.ObjectId,
ref: 'User',
requested: false
},
name: String,
email: String,
mobilenumber: String,
notes: String,
requirestedDate: Date,
status: String,
eventsList: [
{
id: mongoose.Schema.ObjectId,
name: String
}
],
intrestedType: String
}
]
});
Users schema which contain organizer id:
const usersSchema = new Schema({
user_organizationid: {
type: mongoose.SchemaTypes.ObjectId,
required: false,
index: true,
ref: 'Oranizations'
},
user_firstname: String,
user_lastname: String,
user_username: String,
user_mobilenumber: String,
user_mobilenumber_code: String,
user_email: String,
user_role: {
type: mongoose.SchemaTypes.ObjectId,
required: false,
index: true
}
});
Organizer schema:
const organizerSchema = new Schema({
org_name: String,
org_type: String, // organizer,ground , ecommerce seller,
org_category: String, // corporate company,supplier
org_logo: String,
org_address_id: { type: mongoose.Schema.ObjectId, required: false },
org_stack_exchange_id: String,
org_created_dated: Date,
org_updated_date: Date,
org_activte_status: Boolean,
user_hashcode: String
});
So eventually I need:
{
product:'product info',
posted_userid:{
posted_userinfo:'',
organizerInfo:'join with user if there this information'
},
intrestedusers:{
userid:{
userinfo:'',
organizerInfo:'join with user if there this information'
}
}
}
How can I get solution over here?
Inner path must be user_organizationid instead of intresteduserIds.user_organizationid
Products.find({})
.populate({
path: 'intresteduserIds.userId',
populate: {path: 'user_organizationid' }
})
.populate('product_postedby');
Related
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);
I have two models. The first one is UserSchema and the second one is CategorySchema
var UserSchema = Schema({
firstName: {
type: String,
required: true
},
secondName: String,
lastName: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
status: {
type: String,
required: true
},
roles: [{
type: Schema.ObjectId,
ref: 'Role'
}],
publications: [{
title: {
type: String,
},
description: String,
status: {
type: String,
},
createdAt: {
type: Date
},
updatedAt: {
type: Date,
default: Date.now()
},
pictures: [{
name: String
}],
categories: [{
type: Schema.Types.ObjectId,
ref: 'Category'
}],...
the model category is
var CategorySchema = Schema({
name: String,
subcategories: [{
name: String
}]
});
UserSchema has publications. Publications contains an array. Within of publications is categories that contains an array of id of subcategory(subcategory is whithin of CategorySchema)
the problem is when I need to populate categories of UserSchema. Categories of UserSchema have an array of _id of subcategory that belongs to CategorySchema.
I'm having problems accessing user.info.name.familyName in a query. I'm newish to database design and queries and I don't know if its possible.
So here is the schema I have. Perhaps it might need to be redesigned for what I'm trying to do to work.
var UserSchema = mongoose.Schema({
username: {
type: String,
index: true,
required: true,
unique: true
},
password: {
type: String,
required: true
},
email: {
type: String,
required: true,
lowercase: true
},
emailVerified: {
type: Boolean,
default: false
},
info: {
name: {
givenName: String,
familyName: String,
},
address: {
street: String,
city: String,
state: String,
zipCode: String
},
primaryPhone: String,
cellPhone: String,
website: String,
licenseNumber: String,
title: String,
company: String
},
avatar: {
type: String,
default: '/images/avatar/default/contacts-128.png'
},
friends: [{ type: ObjectId, ref: User }],
friendRequests: [{ type: ObjectId, ref: User }]
});
I have a search function that currently searches by username or email and works fine, but I'd like it to also search by the users givenName or familyName. Here is the search function...
module.exports.search = function(searchValue, callback) {
var searchValue = new RegExp(`^${searchValue}`, 'i');
var query = {
$or: [
{username: searchValue},
{email: searchValue}
// {givenName condition here...}
]
}
User.find(query, callback).limit(10);
}
I tried this... and it didn't work
{info.name.givenName: searchValue}
I tried a few other nonsensical things but they also failed...
So to summarize my question, how would I either restructure the schema or query to be able to check the searchValue against user.info.name.givenName?
This way:
{'info.name.givenName': searchValue}
notice the quotes.
You were close :)
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
I am creating a collection w/n mongo using mongoose to populate related users. I have clinicians and patients. I want patients to have an array of clinicians as they are enrolledwith. Later I'll load a dashboard for clincian based on patient user "enrolledWith: "
Likely clincians will provide their email to patients to be marked in rolled. emails are username.
Can I use username for the Schema.Types.username?
var mongoose = require('mongoose');
var userSchema = mongoose.Schema({
name: String,
phoneNumber: String,
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
patient: {
// role: Boolean,
enrolledWith: [
{
type: mongoose.Schema.username,
ref: 'User'
}
],
gameTotalScore: Number,
},
// clinician: {
// role: Boolean
// },
role: {
type: String,
required: true
},
});