How to Populate CartItem schema product details into Order Schema using mongoose - mongodb

I want to populate the details of products in my order. Currently it is only adding product id inside the products array. I tried a couple of methods but none seems to work.
import mongoose from 'mongoose'
const CartItemSchema = new mongoose.Schema({
product: {type: mongoose.Schema.ObjectId, ref: 'Product'},
quantity: Number,
shop: {type: mongoose.Schema.ObjectId, ref: 'Shop'},
status: {type: String,
default: 'Not processed',
enum: ['Not processed' , 'Processing', 'Shipped', 'Delivered', 'Cancelled']}
})
const CartItem = mongoose.model('CartItem', CartItemSchema)
const OrderSchema = new mongoose.Schema({
products: [CartItemSchema],
customer_name: {
type: String,
trim: true,
required: 'Name is required'
},
customer_email: {
type: String,
trim: true,
match: [/.+\#.+\..+/, 'Please fill a valid email address'],
required: 'Email is required'
},
delivery_address: {
street: {type: String, required: 'Street is required'},
city: {type: String, required: 'City is required'},
state: {type: String},
zipcode: {type: String, required: 'Zip Code is required'},
country: {type: String, required: 'Country is required'}
},
payment_id: {},
updated: Date,
created: {
type: Date,
default: Date.now
},
user: {type: mongoose.Schema.ObjectId, ref: 'User'}
})
const Order = mongoose.model('Order', OrderSchema)
export {Order, CartItem}
I tried doing this:
const create = async (req, res) => {
try {
req.body.order.user = req.profile;
console.log(req);
let order = new Order(req.body.order);
let neworder = await Order.findById(order._id)
.populate("products.product", "name price")
.populate("products.shop", "name")
.exec();
// console.log(order);
let result = await order.save();
sendMail(order);
res.status(200).json(result);
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err),
});
}
};
Also tried using this for poulating the product details but doesnt seem to work!
Order.findById(order._id).populate({ path: "products.product", select: "_id name price" })

Related

mongoose: validation error on post request

I'm trying to save a new use in mongodb with mongoose, but I am getting ValidationError: "err": "User validation failed:Path 'email' is required., Path 'fname' is required., Path 'username' is required phone: Path phone .despite I make all the data in the body of the post request so:
this is the userModel:
import mongoose from "mongoose";
const userSchema = new mongoose.Schema({
_id: {type: Number},
name: {type: String, required: true},
fname: {type: String, required: true},
phone: {type: String, required: true},
email: {type: String, required: true, unique: true},
password: {type: String, required: false},
image: {type: String, required: false},
isAdmin: {type: Boolean, default: false, required: true},
}, {
timeStamps: true
});
const User = mongoose.model('User', userSchema);
export default User;
this is the userRouter.js:
import express from "express";
import data from '../../data.js';
import User from "../userModel.js";
import expressAsyncHandler from "express-async-handler";
import {generateToken} from "../../utils.js";
import bcrypt from "bcryptjs";
const userRouter = express.Router();
let allUsers = data.users;
userRouter.get("/seed", expressAsyncHandler(async(req, res)=>{
//await User.remove({});
const createdUsers = await User.insertMany(allUsers);
res.send({createdUsers});
}));
userRouter.post("/register", expressAsyncHandler(async(req, res)=> {
//await User.remove({});
const user = new User({
name: req.body.name,
fname: req.body.fname,
phone: req.body.phone,
email: req.body.email,
});
const createdUser = await user.save();
res.send({
_id: createdUser._id,
name: createdUser.name,
fname: createdUser.fname,
phone: createdUser.phone,
email: createdUser.email,
token: generateToken(createdUser)
})
}))
export default userRouter;

How to create an array of objects with default values?

how to create an array of objects with default values?
const schema = new mongoose.Schema({
login: {type: String, unique: true, required: true},
password: {type: String, required: true},
chatlist: [
{
channelname: {type: String, default: "frontend"},
channelid: {type: String, default: "619a6bfe5b0931f1e5dbaf2c"}
},
{
channelname: {type: String, default: "backend"},
channelid: {type: String, default: "619a71002954ba23a951bb8f"}
},
{
channelname: {type: String, default: "devops"},
channelid: {type: String, default: "619d69c190a85a40893b6522"}
},
]
})
this code above does not work
when a new user register i want to add a default chats to his profile
next thing is the user must be able to add/remove chats from the database
how should i do this? do the chats need to be an objects or documents?
const schema = new mongoose.Schema({
chatlist: {
type: Array,
defualt: []
}
})
so to have a structure in the array I personally would make the request correct using error handling.
for more information visit this site
didnt find any good solution so i just did this
const schema = new mongoose.Schema({
login: {type: String, unique: true, required: true},
password: {type: String, required: true},
chatlist: [{
channelname: String,
channelid: String
}]
})
const user = await MONGO_user.create({
login: login,
password: hashPassword,
})
user.chatlist.push(
{
channelname: "frontend",
channelid: "619a6bfe5b0931f1e5dbaf2c"
}, {
channelname: "backend",
channelid: "619a71002954ba23a951bb8f"
}, {
channelname: "devops",
channelid: "619d69c190a85a40893b6522"
})
await user.save()

how to use find in populate (mongoose)

I want to match a string with employee ID, employee Name , project name
My Models : (employee)
const employeeSchema = new mongoose.Schema(
{
employeeID: { type: String, unique: true, required: true },
departmentID: { type: mongoose.Types.ObjectId, ref: 'Department', default: null },
employeeName: { type: String, required: true },
gender: { type: String, default: "", enum: ["Male", "Female"] },
designation: { type: String, default: "" },
email: { type: String, default: null },
branchName: { type: String, default: "" },
employeeType: { type: String, default: "" },
jobStatus: { type: String, default: "" },
joiningDate: { type: Date, default: "" },
leavingDate: { type: Date, default: "" },
comments: { type: String, default: "" },
managerID: { type: mongoose.Types.ObjectId, ref: 'Employee', default: null },
region: { type: String, default: "" },
}
);
(project)
const projectSchema = new mongoose.Schema(
{
projectName: {type: String, required: true},
tech: {type: String, required: true},
startDate: {type: Date, required:true},
endDate: {type: Date, required:true},
customerID:{type:mongoose.Types.ObjectId, ref:'Customer'}
}
);
(employee-Project)
const employeeProjectSchema = new mongoose.Schema(
{
empID: {type: mongoose.Types.ObjectId, required: true, ref: 'Employee'},
projectID: {type: mongoose.Types.ObjectId, required: true, ref: 'Project'},
allocation: {type: [Number]},
assignStartDate: {type: Date},
assignEndDate: {type: Date},
}
);
I'm running my query from employee-project table which is my main table.
What I wanna do is to match a string with with employeeID and employeeName(employee table) and also with projectName(project table).
What I'm doing right now (which is wrong)
async rpSearch(data): Promise<any> {
try {
const result = await MongooseEmployeeProject.find()
.populate({
path: 'empID',
select: 'employeeID employeeName designation region comments resourceManager',
match:{
type:{data}
}
})
.populate({
path: 'projectID',
select: 'projectName tech startDate endDate customerID'
match:{
type:{data}
})
please guide me
First, path must be the field name of the schema. you set path with "employeeID" but "empID" is correct as employeeProjectSchema's field name.
Second, you set match but there is no type field in your employeeSchema and employeeProjectSchema neither. so you have to remove match.
async rpSearch(data): Promise<any> {
try {
const result = await MongooseEmployeeProject.find()
.populate({
path: 'empID',
select: 'employeeID employeeName designation region comments resourceManager'
})
.populate({
path: 'projectID',
select: 'projectName tech startDate endDate customerID'
})

retrieving array data in mongodb which match id

Below is the schema. i want to get the answers as per matched qid, but i am getting all the answers in the answers array. i have tried almost all the queries but not able to understand why is this happening, if you could give link to other article that will be helpful too.
const id = req.params.id;
Channel.findOne({answer: {qid: {$in: [id]}}})
.then(result => {
console.log(result);
// let userAnswer;
// userAnswer = result.answer.map(i => {
// return {userId: i.userId , userName: i.userId.name, answer: i.answer}
// });
// res.json({ans: userAnswer, question: result.content});
})
.catch(err => {
console.log(err);
});
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const channelSchema = new Schema({
name: {
type: String,
required: true
},
category: {
type: String,
required: true
},
creator: {
type: String,
required: true
},
subscribers: [{type: mongoose.Types.ObjectId, required: true, ref: 'User'}],
content: {
question: [{
title: {type: String, required: true},
userId: {type: mongoose.Types.ObjectId, required: true, ref: 'User'}
}]
},
answer: [{
answer: {type: String, required: true},
qid: {type: mongoose.Types.ObjectId, required: true},
userId: {type: mongoose.Types.ObjectId, required: true, ref: 'User'}
}]
});
const model = mongoose.model('Channel', channelSchema);
module.exports = model;
const id = req.params.id;
return Channel.findOne({answer: {qid: {$in: [id]}}})
.then(snapshot => {
const results = [];
snapshot.forEach(doc => {
results.push({
id: doc.id,
data: doc.data()
});
});
return results;
})
})
.catch(err => {
console.log(err);
});
This is the way when you are going to fetch one array record. Not tested, only to show you how to get single record from collection

MongoDB: How to find the relationships between collections in database

I have a collection of user which has id, firstName, lastName. id field of user collection has been used in another collection.
Is there any way to find all collection which used user id?
user schema:
let userSchema = new mongoose.Schema({
firstName: {
type: String,
trim: true,
required: true
},
lastName: {
type: String,
trim: true,
required: true
}
},
{
timestamps: true,
usePushEach: true
});
training schema:
var trainingSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
name: {type: String, required: true},
institution: {
instituteName: {type: String, required: true},
type: {type: String, required: true},
address: {
country: String,
state: String,
city: String
}
},
startDate: Date,
endDate: Date,
achievements: String,
createdAt: Date,
updatedAt: Date,
endorsers: [{
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
profilePic: {
container: {type: String,default: null},
name: { type: String, default: null }
},
currentPosition: {type: String,default: ""},
currentWorkingCompany: {type: String,default: ""}
}],
});
In above schema userId come from user collection
Note: similar to this which is available in MYSQL:
SELECT
ke.referenced_table_name 'parent table',
ke.referenced_column_name 'parent column',
ke.table_name 'child table',
ke.column_name 'child column',
ke.constraint_name
FROM
information_schema.KEY_COLUMN_USAGE ke
WHERE
ke.referenced_table_name IS NOT NULL
AND table_schema = 'your_db_name'
ORDER BY ke.referenced_table_name;
Source: here
You probably need MySQL function named join. In MongoDB it is named $lookup and it is part of aggregate function.