im trying to insert data in mongodb using express in the format as below which im not able to achieve.
I need to enter multiple product and serial no in the data field. please help!
[
{
"_id": "5cbabbd7545ac20f7c912e6a",
"refno1": "REF1",
"refno2": "REF2",
"prodregdate": "2019-04-09T00:00:00.000Z",
"data": [
{
"_id": "5cbabbd7545ac20f7c912e6b",
"product": "5cb86b45cfafaa1860e29b2a",
"serialno": "s123"
},
{ // this data im not able to enter how to do it
"_id": "5cbabbd7545ac20f7c912e6b",
"product": "5cb86b45cfafaa1860e29b2a",
"serialno": "s123"
},
],
"customer": {
"_id": "5c98bb0a42207b16d8fbd3cf",
"customername": "Raghav Update"
},
"customertype": {
"_id": "5c7a1a1d4913fa08ac75c027",
"customertype": "Government "
},
"__v": 0
}
]
// My Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ProductRegistrationSchema = new Schema({
//Product Details
_id: { type: mongoose.Schema.Types.ObjectId },
refno1: { type: String },
refno2: { type: String },
data: [{
product: {
type: mongoose.Schema.Types.ObjectId,
ref: "product"
},
//DATES
//OEM
oemwarrantyfrom: { type: Date },
oemwarrantyto: { type: Date },
//SERVICE PROVIDER
warrantyfrom: { type: Date },
warrantyto: { type: Date },
serialno: { type: String },
}],
prodregdate: { type: Date },
//Details of Customer buying the product
customer: {
type: mongoose.Schema.Types.ObjectId,
ref: "customer"
},
customertype: {
type: mongoose.Schema.Types.ObjectId,
ref: "customertype"
},
department: {
type: mongoose.Schema.Types.ObjectId,
ref: "customersubdepartment"
},
remarks: { type: String },
entrydate: {
type: Date,
dafault: Date.now
}
module.exports = ProductRegistration = mongoose.model('productregistration', ProductRegistrationSchema);
// My Routes just for add
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const Product = require("../../../models/Master/Products");
//importing the model of ProductRegistrationSchema
const ProdReg = require('../../../models/Entries/ProductRegistration');
//Creating a new ProductRegistration Data
router.post('/add', (req, res)=>{
const newProdReg = new ProdReg({
_id: new mongoose.Types.ObjectId(),
refno1: req.body.refno1,
refno2: req.body.refno2,
prodregdate: req.body.prodregdate,
data: {
product: req.body.productid,
oemwarrantyfrom: req.body.oemwarrantyfrom,
oemwarrantyto: req.body.oemwarrantyto,
warrantyfrom: req.body.warrantyfrom,
warrantyto: req.body.warrantyto,
serialno: req.body.serialno,
},
customer: req.body.customerid,
customertype: req.body.customertypeid,
department: req.body.customersubdepartmentid,
remarks: req.body.remarks
// deliverydate: req.body.deliverydate,
// address: req.body.address,
// assignedto: req.body.employeesid,
// warrantyprovider: req.body.serviceproviderid,
// oemwarrantyprovider: req.body.oemcompanyid,
// warrantystartdate: req.body.warrantystartdate,
// warrantyexpiredate: req.body.warrantyexpiredate,
});
newProdReg.save().then(prodreg => res.json(prodreg));
});
im not able to enter 2 product and serial no in the data field. One one is getting entered.
Firstly make your request JSON in the proper format if you want to insert two products which you are getting from request data.
For example, your request JSON should be in the flowing format:
{"refno1":"x", "refno2": "y", "prodregdate": "2019-04-19T18:30:00.000Z","data": [{"product": "product_1_object_id","oemwarrantyfrom":"2019-04-19T18:30:00.000Z", "oemwarrantyto": "2019-04-19T18:30:00.000Z","warrantyfrom":"2019-04-19T18:30:00.000Z", "warrantyto":"2019-04-19T18:30:00.000Z","serialno":"123" },{"product": "product_2_object_id","oemwarrantyfrom":"", "oemwarrantyto": "2019-04-19T18:30:00.000Z","warrantyfrom":"2019-04-19T18:30:00.000Z", "warrantyto":"2019-04-19T18:30:00.000Z","serialno":"456" }],"customersubdepartmentid":"departement_object_id","customerid":"customer_object_id","customertypeid":"customer_type_object_id","remarks":"anything"}
If you are using POSTMAN then you can try this JSON in the "raw" option.
Then in your code, it should be like below:
router.post('/add', (req, res)=>{
const newProdReg = new ProdReg({
_id: new mongoose.Types.ObjectId(),
refno1: req.body.refno1,
refno2: req.body.refno2,
prodregdate: req.body.prodregdate,
data: req.body.data, // This will be type array with two products details
customer: req.body.customerid,
customertype: req.body.customertypeid,
department: req.body.customersubdepartmentid,
remarks: req.body.remarks
});
newProdReg.save().then(prodreg => res.json(prodreg));
});
Please match your request param with JSON I took it from your schema.
Related
I have 4 level nested schema:
Framework has Domain referenced to it, Domain has control referenced to it, and Control has SubControl referenced to it.
Now I have been searching for a while and I keep getting confused.
First question: is it possible to post all the data from the framework it self?
Second question: I have used referencing with ID approach, should I switch to using subDocuments?
Framework Schema:
const FrameworkSchema = new Schema({
name: {
type: String,
trim: true
},
description: {
type: String,
trim: true
},
domain: [{
domain: {type: Mongoose.Schema.Types.ObjectId, ref: 'Domain'}
}],
updated: Date,
created: {
type: Date,
default: Date.now
}
});
module.exports = Mongoose.model('Framework', FrameworkSchema);
Domain Schema:
const DomainSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
domainNo: {
type: String,
trim: true
},
domainName: {
type: String,
trim: true
},
domainDescription: {
type: String,
trim: true
},
framework: {
type: Mongoose.Schema.Types.ObjectId,
ref: 'Framework'
},
control: [{
control: {type: Mongoose.Schema.Types.ObjectId, ref: 'Control'}
}],
updated: Date,
created: {
type: Date,
default: Date.now
}
});
module.exports = Mongoose.model('Domain', DomainSchema);
My control Schema:
const ControlSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
mainControl: {
type: String
},
subControl: [{
subControlNo: {type: Mongoose.Schema.Types.String, ref: 'SubControl'}
}],
controlDescription: {
type: String,
trim: true
},
updated: Date,
created: {
type: Date,
default: Date.now
}
});
module.exports = Mongoose.model('Control', ControlSchema);
My SubControl Schema
const SubControlSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
subControlNo: {
type: [String]
},
updated: Date,
created: {
type: Date,
default: Date.now
}
});
module.exports = Mongoose.model('SubControl', SubControlSchema);
Now I'm trying to post this nested documents from the framework api:
router.post(
'/add',
auth,
role.checkRole(role.ROLES.Admin), async (req, res) => {
try {
const subControl = new SubControl({...req.body});
const subControlDoc = await subControl.save();
const control = new Control({...req.body}); // take the control data
control.subControl.push(subControlDoc._id); // take the subControl and push the ID into the control
const controlDoc = await control.save();
//make the subcontrol pushed into control
// make control pushed in domain
const domain = new Domain({...req.body});
domain.control.push(controlDoc._id);
const domainDoc = await domain.save();
const framework = new Framework({...req.body});
framework.domain.push(domainDoc._id);
const frameworkDoc = await framework.save(); //save the framework + domain's ID
res.status(200).json({
success: true,
message: `Framework has been added successfully!`,
framework: frameworkDoc
});
} catch (error) {
res.status(400).json({
error
// error: 'Your request could not be processed. Please try again.'
});
}
}
);
Now I'm using push to push the data as an array, not sure if this the right approach, and Is it possible to post the all the data from the framework api?
Tried to post this from postman:
{
"name": "ISO780001",
"description": "frameworkDescription",
"domain":
[
{
"domainNo": "11",
"domainName": "domainName00",
"domainDescription": "domaindescu0",
"control": [{
"mainControl": "1-4",
"subControl": [{
"subControlNo": "1-4-1"
},
{
"subControlNo": "1-4-2"
}],
"controlDescription": "controlDescriptionTest"
},
{
"mainControl": "1-4",
"subControl": [{
"subControlNo": "1-4-1"
},
{
"subControlNo": "1-4-2"
}],
"controlDescription": "controlDescriptionTest"
}
]
},
{
"domainNo": "1-2",
"name": "domainName00",
"description": "domaindescu0",
"control": {
"mainControl": "1-4",
"subControl": [{
"subControlNo": "1-4-1"
},
{
"subControlNo": "1-4-2"
}],
"controlDescription": "controlDescriptionTest"
}
}
]
}
Only the id's of the domain, control, and subControl get saved in mongodb, is this the how it works can I post all the data from one model in this case the framework? or should I use embedded approach ?
What I will do in scenario where i have alot of references (mongoose name it ref by the way, which allows you to populate).
Example of a frameWork schema with domain reference.
const frameworkSchema = mongoose.Schema({
domains: [{type: mongoose.Schema.Types.ObjectId, ref: 'Domain'}],
})
const FrameworkModel = mongoose.model('Framework', frameworkSchema)
Domain above refers to a Domain model. We can create a domain model now.
const domainSchema = mongoose.Schema({
_id: { type: mongoose.Schema.Types.ObjectId } //this is the default
})
const DomainModel = mongoose.model('Domain', domainSchema);
Example Usage - We want to get all the domain information related to a specific schema.
const results = FrameworkModel.findOne({ _id: 'some framework id'}).populate('domains').lean({ virtuals: true });
The results will be something like
{
_id: 'some framework id',
name: 'name of framework',
domains: [
{
_id: 'id of domain 1',
name: 'example.com'
},
{
_id: 'id of domain 2',
name: 'example2.com'
}
]
}
You can also explore virtuals to see how you can maintain your framework, domains and other controls as separate collections, in order to easily reference to them. This is a better design than nesting multiple levels in a single document.
Unless where necessary, using separate collections has more benefits than using subdocuments. (easier to find documents and easier to update documents, and of course, more performant)
little bit stuck with mongoose. I want to get all users projects where he's added. Tried few options, but in all of them I receive an empty array. So the main question is it possible somehow to find all project by filtering/finding the Project model?
This is how looks my default response to understand what I'm looking for:
{
"_id": "61a8bc4e8e24f10ac7a7288d",
"name": "Random project",
"description": "Random project desc",
"maxWorkingEmployees": 5,
"status": "Paused",
"createdBy": {
"_id": "61a66578f2dabf7555bcf4ab",
"email": "Henrikas#furniture1.eu",
"role": "Owner"
},
"currentlyWorkingEmployees": [
{
"username": "Ema",
"role": "Employee",
"_id": "61a8e0423140ecce769dc971"
}
],
"createdAt": "2021-12-02T12:30:06.461Z",
"updatedAt": "2021-12-02T15:11:51.361Z",
"__v": 0
}
Project model:
const mongoose = require('mongoose');
const SingleUserSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
role: {
type: String,
required: true,
},
});
const ProjectSchema = new mongoose.Schema(
{
name: {
type: String,
required: [true, 'Provide project name'],
minlength: 5,
},
description: {
type: String,
required: [true, 'Provide description about the project'],
},
maxWorkingEmployees: {
type: Number,
required: [
true,
'Provide maximum number of employees working on this project',
],
},
currentlyWorkingEmployees: [SingleUserSchema],
status: {
type: String,
enum: ['Pending', 'In progress', 'Paused', 'Delayed', 'Completed'],
default: 'Pending',
},
createdBy: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true,
},
},
{ timestamps: true }
);
module.exports = mongoose.model('Project', ProjectSchema);
Here's my controller and my first try:
const getMyProjects = async (req, res) => {
const userId = req.user.userId;
const projects = await Project.find({
currentlyWorkingEmployees: { _id: userId },
});
res.json({projects});
};
Second shot after reading some articles
const getMyProjects = async (req, res) => {
const userId = req.user.userId;
const projects = await Project.aggregate([
{
$match: {
currentlyWorkingEmployees: { _id: userId },
},
},
]);
};
As I said in the comment you can do it accessing to the internal object of the schema with an string accessing to it child object.
Project.find({'currentlyWorkingEmployees._id': userId})
I am trying to populate the task_item you can see below but I am not getting the result.
{
"msg": "User not exists",
"cart": {
"_id": "5ec0c28cf494113aa06c99d9",
"task_items": [
{
"quantity": 1,
"_id": "5ec0c28cf494113aa06c99da",
**"task_item": "5ebe499d7e657d322c39345f"**
},
{
"quantity": 1,
"_id": "5ec0d018ecc2411cf40bceae",
**"task_item": "5ebe499d7e657d322c39345f"**
},
{
"quantity": 1,
"_id": "5ec0d4abfac25246689d15a7",
**"task_item": "5ec0d4a2fac25246689d15a6"**
}
],
"crreated": "2020-05-17T04:50:20.869Z",
"user": {
"_id": "5ebe4d5d440ab504c8a0d128",
"name": "Ajit",
"phone": "9668854799",
"avatar": "//www.gravatar.com/avatar/20c3d59b7049c9ad85cd33e2ebd3151c?s=200&r=pg&d=mm",
"password": "$2a$10$FdH7QzoqOHnVoLbbm9GaUOLaJbTTqPWzFWeFACkea2NWlrJrFxZ5a",
"createdAt": "2020-05-15T08:05:49.418Z",
"__v": 0
},
"__v": 2
}
}
Below is the Cart.js Model
const monngose = require('mongoose');
const Schema = monngose.Schema;
const CartSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'user'
},
task_items: [{
task_item: {type:Schema.Types.ObjectId, ref: 'SubTaskCategoryItem'},
quantity: { type: Number, default: 1 }
}],
totalPrice: {
type: Number
},
item: {
type: Schema.Types.ObjectId,
ref: 'SubTaskCategoryItem'
},
crreated: {
type: Date,
default: Date.now
}
});
module.exports = monngose.model('Cart', CartSchema);
SubCategoryItem.js - This is the item model
const mongoose = require('mongoose');
const SubTaskItemSchema = new mongoose.Schema({
subtaskItemName: {
type: String,
required: true,
unique: true,
trim: true
},
subtaskItemImage: {
type: String
},
subtaskItemTime: {
type: String
},
description: {
type: String,
trim: true
},
subtaskItemPrice: {
type: String
},
subtaskItemStatus: {
type: Boolean,
default: true
},
subtask: {
type: mongoose.Schema.ObjectId,
ref: 'SubTask',
required: true
},
createdAT: {
type: Date,
default: Date.now
}
})
module.exports = mongoose.model("SubTaskCategoryItem", SubTaskItemSchema);
Here I am trying to populate - cart.js router
const express = require('express');
const Cart = require('../../models/admin/Cart');
const checkJwt = require('../../middleware/admin/user_auth');
const TaskItem = require('../../models/admin/SubTaskCategoryItem');
const router = express.Router();
router.post('/add_cart', checkJwt, (req, res, next) => {
const product_id = req.query.productId;
const quantity = req.query.quantity;
const user = req.user.id;
router.get('/get_cart', checkJwt, (req, res, next) => {
Cart.findOne({ user: req.user.id })
.populate({
path: 'user'
})
.populate({
path: 'item'
})
.exec((err, cart) => {
res.json({
msg: 'User not exists',
cart: cart
});
})
})
module.exports = router;
I am trying to populate the task_item but I am not able to do. If anyone knows please see this code.
const Schema = mongoose.Schema
const logEntry = new Schema({
no:{
type: Number
},
type: {
type: String
},
createdAt: {
type: Date,
default: Date.new
},
ingredients: {
type: String
},
process: {
type: String
},
cook: [{
type: mongoose.Schema.Types.ObjectId, ref: 'shef'
}]
});
const shefEntry = new Schema({
name: {
type: String
},
dish: {
type: mongoose.Schema.Types.ObjectId, ref: 'logDetails'
}
})
const foodItem = mongoose.model('logDetails', logEntry);
const Cook = mongoose.model('shef', shefEntry);
This is my Schema.
app.route('/log_entries')
.get(controller.index)
.post(controller.create)
This is my route.
exports.index = function(req, res){
logEntry.find({})
.populate('Cook')
.exec(function(err, logEntry){
if(err) res.send(err);
res.json(logEntry);
})
}
This is my controller where I'm trying to populate. My requirement is to populate the name of cook into recipe collection but I get the result as
{
"cook": [],
"_id": "5dafdbd8b9cefa2670ab73c7",
"no": 101,
"type": "Non-Veg",
"ingredients": "Gobi,Paneer",
"process": "Go on with the manual",
"__v": 0
},
{
"cook": [],
"_id": "5dbb76bf5356143124d3afbd",
"no": 101,
"type": "veg",
"ingredients": "Gobi,Paneer",
"process": "Go on with the manual",
"__v": 0
}
Can anyone explain how to use mongoose populate? I mean about how to join 2 collections and which collection to make main collection. Also refer me with some good website where I can learn more about mongoose join.
Cook replace with cook
exports.index = function(req, res){
logEntry.find({})
.populate('cook')
.exec(function(err, logEntry){
if(err) res.send(err);
res.json(logEntry);
}) }
I want to get all the posts with their author details from user model. I am using mongoDB lookup. But getting an empty array. I am matching author.uid from post to _id of user.
I want to get all the posts with their author details from user model. I am using mongoDB lookup. But getting an empty array. I am matching author.uid from post to _id of user.
//Post Model
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const postSchema = new Schema({
category : {
type: String
},
content: {
type: String
},
caption: {
type: String
},
tags: [{
type: String
}],
createdAt: {
type: Number,
required: true
},
author: {
uid:{
type: String,
required: true
},
name:{
type: String
}
},
likes:[{
type:String
}],
comments:[{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}]
});
module.exports = mongoose.model('Post', postSchema);
//User Model
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
_id: {
type: String,
required: true
},
name:{
type: String,
required: true
},
avatar:{
type:String
},
bio:{
type: String
},
followers:[
{
type: String
}
],
followings:[
{
type: String
}
],
posts:[{
type: mongoose.Schema.Types.ObjectId,
ref: "Post"
}]
});
module.exports = mongoose.model('User', userSchema);
//Node js
const express = require('express');
const router = express.Router();
const Post = require('../../models/Post');
const User = require('../../models/user');
router.get('/', (req, res) => {
Post.aggregate([
{
$lookup:
{
from: 'User',
localField: "author.uid",
foreignField: "_id",
as: "creator"
}
}
]).exec((err, result) => {
if (err) {
console.log("error" ,err)
}
if (result) {
console.log(JSON.stringify(result));
}
});
});
//Output
{"_id":"5b9c7f30d",
"author": {"uid":"y08RxtsHe","name":"Sujoy Saha"},
"tags": ["#lo"],
"likes":[], "comments[],
"category":"image","content":"jsdnvs","caption":"standing
\n#lol","createdAt":1536982759517,"__v":0,"creator":[]}
You can see, i am getting empty creator array. Please help me out.
mongoose.js pluralizes (adds 's' after your model name) when it creates a collection in MongoDb.
Can you try with from: 'users' in your $lookup clause?