Mongoose not saving my new document in array - node.js

so my code is that:
router.put("/add-task/:id", auth, boardAuth, async (req, res) => {
const listId = req.params.id;
try {
const board = await Board.findOne({ _id: req.board._id });
if (!board) return res.status(404).send("no such board");
const list = await List.findOne({ _id: listId });
if (!list) return res.status(404).send("List not found");
const task = new Task({
text: req.body.text,
});
list.cards.push(task);
board.boardLists.forEach((list) => {
if (listId.toString() === list._id.toString()) {
list.cards.push(task);
} else {
console.log('no task');
}
});
await board.save();
await list.save();
res.send(board);
} catch (err) {
console.log(err);
}
});
i want to save task (card) inside the board object.
my res is that:
"boardMembers": [
"5f326c2e8b906103642af93b"
],
"boardLists": [
{
"cards": [
{
"_id": "5f63147c14ba4d4464e263ea",
"text": "two"
}
],
"_id": "5f622ca82e6edf1eb8dab7b7",
"title": "list number one",
"__v": 0
}
],
"_id": "5f622c702e6edf1eb8dab7b6",
"boardName": "board one",
"boardPassword": "123456",
"boardCreator": "5f326c2e8b906103642af93b",
"g_createdAt": "2020-09-16T15:17:04.012Z",
"__v": 2
the problem is when i refresh or sending request again the array of cards is empty again. its not saving the last doc ("two") i added. what i am doing worng here?
update - schema of board
this is the Boardschema
const boardSchema = new mongoose.Schema({
boardName: {
type: String,
required: true,
maxLength: 255,
minLength: 2,
},
boardPassword: {
type: String,
required: true,
minlength: 6,
maxlength: 255,
},
g_createdAt: { type: Date, default: Date.now },
boardCreator: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
boardMembers: Array,
boardLists: Array,
});
const Board = mongoose.model("Board", boardSchema);
this is the List schema
const listSchema = new mongoose.Schema({
title: {
type: String,
required: true,
maxLength: 255,
minLength: 1,
},
cards: Array,
});
const List = mongoose.model("List", listSchema);
this is the Task schema:
const taskSchema = new mongoose.Schema({
text: {
type: String,
required: true,
maxLength: 1024,
minLength: 2,
},
taskOf: { type: mongoose.Schema.Types.ObjectId, ref: "List" },
});

Cascade your reference fields in the opposite direction:
const boardSchema = new mongoose.Schema({
boardName: {
type: String,
required: true,
maxLength: 255,
minLength: 2,
},
boardPassword: {
type: String,
required: true,
minlength: 6,
maxlength: 255,
},
g_createdAt: { type: Date, default: Date.now },
boardCreator: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
boardMembers: Array,
boardLists: [{ type: mongoose.Schema.Types.ObjectId, ref: "lists" }],
});
const listSchema = new mongoose.Schema({
title: {
type: String,
required: true,
maxLength: 255,
minLength: 1,
},
cards: [{ type: mongoose.Schema.Types.ObjectId, ref: "tasks" }],
});
const taskSchema = new mongoose.Schema({
text: {
type: String,
required: true,
maxLength: 1024,
minLength: 2,
},
});
const List = mongoose.model("lists", listSchema);
const Board = mongoose.model("boards", boardSchema);
const Task = mongoose.model("tasks", boardSchema);
See if data gets persisted following this logic

Related

One to many with MongoDB (Mongoose) with .populate()

What I am doing
I am trying to return a player by playerId with all ref items from each object in it's schema. In this example, I am specifically talking about the players inventory.
How can I return all reference items and their properties?
In my service file, I am getting my player with:
/**
* Get a player by playerId
* #param playerId
* #returns {Promise<*>}
*/
module.exports.getPlayer = async (playerId) => {
return await Player.findOne({ playerId: playerId }).populate('inventory');
};
And this is my returned JSON
{
"success": true,
"data": {
"_id": "63bb1f3ec17d33f4d2a87194",
"playerId": 4183489039,
"name": "AlanGreyjoy",
"currency": {
"copper": 500,
"silver": 10,
"gold": 0
},
"online": false,
"inventory": [
{
"currentDurability": 0,
"item": "63ba0c54407456969ba38615",
"amount": 1,
"_id": "63bc4fa070eaa247288e3573",
"createdAt": "2023-01-09T17:32:16.643Z",
"updatedAt": "2023-01-09T17:32:16.643Z"
}
],
"bank": [],
"questTracker": [],
"friends": [],
"knownRecipes": [],
"jobs": [],
"createdAt": "2023-01-08T19:53:34.903Z",
"updatedAt": "2023-01-09T17:32:16.643Z",
"__v": 1
}
}
As you can see, the item in the inventory array is not populating with the ref item from the items collection.
I have googled and googled and tried so many different things, but nothing is working.\
The item does exist in the db
My Models
My Player.model.js
const mongoose = require('mongoose');
const toJSON = require('../plugins/mongoToJson');
const Schema = mongoose.Schema;
const questTracker = new Schema(
{
quest: { type: mongoose.Schema.Types.ObjectId, ref: 'Quest' },
complete: { type: Boolean, default: false },
},
{ timestamps: true }
);
const friends = new Schema(
{
player: { type: mongoose.Schema.Types.ObjectId, ref: 'Player' },
isFavorite: { type: Boolean, default: false },
},
{ timestamps: true }
);
const knownRecipes = new Schema(
{
recipe: { type: mongoose.Schema.Types.ObjectId, ref: 'Recipe' },
},
{ timestamps: true }
);
const jobs = new Schema(
{
job: { type: mongoose.Schema.Types.ObjectId, ref: 'Job' },
},
{ timestamps: true }
);
const inventoryItems = new Schema(
{
item: { type: mongoose.Schema.Types.ObjectId, ref: 'Item' },
amount: { type: Number, default: 0 },
currentDurability: { type: Number, default: 0 },
},
{ timestamps: true }
);
const bank = new Schema(
{
item: { type: mongoose.Schema.Types.ObjectId, ref: 'Item' },
amount: { type: Number, default: 0 },
},
{ timestamps: true }
);
const playerSchema = new Schema(
{
playerId: {
type: Number,
unique: true,
required: true,
},
name: {
type: String,
required: true,
unique: true,
},
currency: {
type: Object,
default: {
copper: 500,
silver: 10,
gold: 0,
},
},
inventory: [inventoryItems],
bank: [bank],
questTracker: [questTracker],
friends: [friends],
knownRecipes: [knownRecipes],
jobs: [jobs],
online: {
type: Boolean,
default: false,
},
},
{ timestamps: true }
);
const PlayerModel = mongoose.model('player', playerSchema);
module.exports = PlayerModel;
My Item.model.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const toJSON = require('../plugins/mongoToJson');
const vendorPriceSchema = mongoose.Schema({
copper: {
type: Number,
default: 0,
},
silver: {
type: Number,
default: 0,
},
gold: {
type: Number,
default: 0,
},
});
const itemSchema = new Schema(
{
title: {
type: String,
required: true,
unique: true,
},
assetId: {
type: Number,
required: true,
},
description: {
type: String,
},
equipDescription: {
type: String,
},
additionalDescription: {
type: String,
},
consumableUseDescription: {
type: String,
},
itemLevel: {
type: Number,
},
requiredLevel: {
type: Number,
},
type: {
type: String,
required: true,
},
subtype: {
type: String,
required: true,
},
stamina: {
type: Number,
},
intellect: {
type: Number,
},
criticalStrike: {
type: Number,
},
agility: {
type: Number,
},
mastery: {
type: Number,
},
maxDurability: {
type: Number,
},
vendorPrice: { vendorPriceSchema },
minDamage: {
type: Number,
default: 0,
},
maxDamage: {
type: Number,
default: 0,
},
speed: {
type: Number,
},
maxStack: {
type: Number,
},
},
{ timestamps: true }
);
const ItemModel = mongoose.model('Item', itemSchema);
module.exports = ItemModel;
You can use something like this:
module.exports.getPlayer = async (playerId) => {
return await Player.findOne({ playerId: playerId }).populate({
path: "inventory",
populate: {
path: "item",
},
});
};

Joining two mongoose collections with many to many relationship

I have two models which are Product & Category with many to many relationship.
Following are my models
This is the model for Product
//Product.js
const mongoose = require("mongoose");
const { Schema } = mongoose;
const { ObjectId } = Schema;
const product = {
name: {
type: String,
required: true,
trim: true,
minlength: [2, "Too short"],
maxlength: [32, "Too long"],
unique: true,
},
slug: {
type: String,
unique: true,
lowercase: true,
index: true,
},
category: [
{
type: ObjectId,
ref: "Category",
},
],
description: {
type: String,
maxlength: 200,
},
price: {
type: Number,
required: true,
trim: true,
maxlength: 32,
},
shipping: {
type: String,
enum: ["Yes", "No"],
},
color: [
{
type: String,
enum: ["Black", "Brown", "Silver", "White", "Blue"],
},
],
sold: {
type: Number,
default: 0,
},
quantity: {
type: Number,
},
images: [
{
public_id: String,
url: String,
},
],
};
const productSchema = new Schema(product, { timestamps: true });
const Product = mongoose.model("Product", productSchema);
module.exports = Product;
This is the model for Category
//Category.js
const mongoose = require("mongoose");
const { Schema } = mongoose;
const { ObjectId } = Schema;
const category = {
name: {
type: String,
required: true,
trim: true,
max: 32,
unique: true,
},
subCategories: [
{
type: Schema.Types.ObjectId,
ref: "Category",
},
],
parent: {
type: Schema.Types.ObjectId,
ref: "Category",
},
products: [
{
type: ObjectId,
ref: "Product",
},
],
slug: {
type: String,
required: "URL can't be empty",
unique: true,
},
};
const categorySchema = new Schema(category, { timestamps: true });
//Validate the slug to ensure proper url is given
// categorySchema.path("slug").validate((val) => {
// urlRegex =
// /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-/]))?/;
// return urlRegex.test(val);
// }, "Invalid URL.");
const autoPopulateChildren = function (next) {
this.populate("subCategories");
// this.populate("parent");
next();
};
categorySchema
.pre("findOne", autoPopulateChildren)
.pre("findById", autoPopulateChildren)
.pre("find", autoPopulateChildren);
const Category = mongoose.model("Category", categorySchema);
module.exports = Category;
So, product has a foreign keys of category in array of category. Whereas Category has foreign keys of product in array of products. How do you join them ?
I tried this & it doesn't work. It returns empty object
router.get("/products", [RequireSignIn], async (req, res) => {
try {
const products = await Product.find({}).aggregate({
$lookup: {
from: "Category",
localField: "category",
foreignField: "_id",
as: "ProductCategories",
},
});
return res.status(200).send(products);
} catch (error) {
return res.status(200).send(error);
}
});

Nodejs mongoose get results from multiple collections in one query

I'm very new to Nodejs and MongoDB, I have 3 collections one for chapter,one for lecture and one for asset
every course have chapters, every chapter has array of lectures and every lecture have assets
so I want to get chapters by courseId which already done, and inside chapter to get its lectures and in every lecture to get its assets
Course:
const mongoose = require('mongoose');
var Double = require('#mongoosejs/double');
const courseSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
requirements: {
type: String,
},
code: {
type: String,
required: true,
},
coverPhoto: {
type: String,
required: false,
},
description: {
type: String
},
instructor:{
type:mongoose.Schema.Types.ObjectId,
ref:'User',
required:true
},
category:{
type:mongoose.Schema.Types.ObjectId,
ref:'Category',
required:true
},
learns: [{
type: String
}],
subCategory:{
type:mongoose.Schema.Types.ObjectId,
ref:'SubCategory',
required:true
},
isCoaching: {
type: Boolean,
default: false,
},
isFree: {
type: Boolean,
default: false,
},
price: {
type: Double,
default: 0,
},
rating: {
type: Double,
default: 0,
},
isPublished: {
type: Boolean,
default: false,
},
dateCreated: {
type:Date,
default:Date.now,
},
});
exports.Course = mongoose.model('Course', courseSchema);
exports.courseSchema = courseSchema;
Chapter:
const mongoose = require('mongoose');
const chapterSchema = new mongoose.Schema({
course:{
type:mongoose.Schema.Types.ObjectId,
ref:'Course',
required:true
},
title: {
type: String,
required: true,
},
sort_order: {
type: Number,
default: 1,
},
is_published: {
type: Boolean,
default: true,
},
});
exports.Chapter = mongoose.model('Chapter', chapterSchema);
exports.chapterSchema = chapterSchema;
Lecture:
const mongoose = require('mongoose');
const lectureSchema = new mongoose.Schema({
chapter:{
type:mongoose.Schema.Types.ObjectId,
ref:'Chapter',
required:true
},
title: {
type: String,
required: true,
},
sort_order: {
type: Number,
default: 1,
},
is_published: {
type: Boolean,
default: true,
},
});
exports.Lecture = mongoose.model('Lecture', lectureSchema);
exports.lectureSchema = lectureSchema;
Asset:
const mongoose = require('mongoose');
const assetSchema = new mongoose.Schema({
lecture:{
type:mongoose.Schema.Types.ObjectId,
ref:'Lecture',
required:true
},
title: {
type: String,
required:true
},
asset_type: {
type: String
},
description: {
type: String,
require:true
},
file_url: {
type: String,
require:true
},
page_number: {
type: Number,
default:1
},
time_estimation: {
type: String,
require:true
},
is_external: {
type: Boolean,
default: false,
},
is_published: {
type: Boolean,
default: true,
},
});
exports.Asset = mongoose.model('Asset', assetSchema);
exports.assetSchema = assetSchema;
Get Chapters of a course
router.get(`/`, async (req, res) => {
let course_filter = {};
if (req.query.course) {
course_filter = {course:req.query.course};
}
const chapterList = await Chapter.find(course_filter).populate('lecture').sort('sort_order');
if (!chapterList) {
res.status(500).json({ success: false });
}
res.send(chapterList);
});
using simple aggregation:
const chapterList = await Chapter.aggregate([{
$lookup: {
from: 'lectures',
localField: '_id',
foreignField: 'chapter',
as: 'lectures'
}}]);
You have to nest the populate in another populate:
router.get(`/`, async (req, res) => {
let course_filter = {};
if (req.query.course) {
course_filter = { course: req.query.course };
}
const chapterList = await Chapter.find(course_filter)
.populate({ path: 'lectures', populate: { path: 'assets' } })
.sort('sort_order');
if (!chapterList) {
res.status(500).json({ success: false });
}
res.send(chapterList);
});
You have to ensure that you have set a virtual for 'assets' prop in lectureSchema accordingly. I assume you have also done it for your 'Chapter' schema.
If not, you have do add the following:
virtual for Chapter schema:
chapterSchema.virtual('lectures', {
ref: Lecture.collection.collectionName,
localField: '_id',
foreignField: 'chapter'
});
virtual for Lecture schema:
lectureSchema.virtual('assets', {
ref: Asset.collection.collectionName,
localField: '_id',
foreignField: 'lecture'
});

Mongoose problem adding subchild to a document

I am brand new to MongoDB and to Node JS.
I have got a problem adding a child to a document in Mongo.
The database has a collection cald weddings and in each document is one wedding.
The wedding documents also contain the guests, something like this:
wedding 1
- guest 1
- guest 2
wedding 2
- guest 3
- guest 4
etc.
Now I am having trouble adding guests to the weddings.
Here is my Model:
const Joi = require('joi');
const mongoose = require('mongoose');
const guestSchema = new mongoose.Schema({
name: {
type: String,
required: true,
minlength: 2,
maxlength: 255
},
surname: {
type: String,
required: true,
minlength: 2,
maxlength: 255
},
plz: {
type: String,
minlength: 2,
maxlength: 10
},
confirmed: { type: Boolean, default: false },
type: Number,
questlink_id: Number,
confirmedDate: Date,
hasExtraTable: Boolean
});
const weddingSchema = new mongoose.Schema({
title: {
type: String,
required: true,
minlength: 2,
maxlength: 255
},
nameA: {
type: String,
required: true,
minlength: 2,
maxlength: 255
},
nameB: {
type: String,
required: true,
minlength: 2,
maxlength: 255
},
surnameA: {
type: String,
required: true,
minlength: 2,
maxlength: 255
},
surnameB: {
type: String,
required: true,
minlength: 2,
maxlength: 255
},
location: {
type: String,
required: true,
minlength: 2,
maxlength: 255
},
street: {
type: String,
required: true,
minlength: 2,
maxlength: 50
},
streetnumber: {
type: String,
required: true,
minlength: 1,
maxlength: 50
},
plz: {
type: String,
required: true,
minlength: 2,
maxlength: 10
},
city: {
type: String,
required: true,
minlength: 2,
maxlength: 50
},
country: {
type: String,
required: true,
minlength: 2,
maxlength: 50
},
telefon: {
type: String,
minlength: 5,
maxlength: 50
},
email: {
type: String,
minlength: 5,
maxlength: 255
},
payed: { type: Boolean, default: false },
user_id: {
type: String,
required: true
},
weddingDate: {
type: Date
},
registerUntilDate: {
type: Date
},
tableSize: {
type: Number
},
link: {
type: String,
unique: true,
default: null
},
guest: [ guestSchema ]
});
const Wedding = mongoose.model('wedding',weddingSchema);
function validateWedding(wedding) {
const schema = {
title: Joi.string().min(2).max(255).required(),
nameA: Joi.string().min(5).max(50).required(),
surnameA: Joi.string().min(5).max(50).required(),
nameB: Joi.string().min(5).max(50).required(),
surnameB: Joi.string().min(5).max(50).required(),
location: Joi.string().min(5).max(50).required(),
street: Joi.string().min(5).max(50).required(),
streetnumber: Joi.string().min(5).max(50).required(),
city: Joi.string().min(5).max(50).required(),
plz: Joi.string().min(5).max(50).required(),
country: Joi.string().min(5).max(50).required(),
};
return Joi.validate(wedding, schema);
}
function validateGuest(guest) {
const schema = {
name: Joi.string().min(5).max(50).required(),
surname: Joi.string().min(5).max(50).required(),
plz: Joi.string().min(5).max(50).required()
};
return Joi.validate(guest, schema);
}
exports.guestSchema = guestSchema;
exports.weddingSchema = weddingSchema;
exports.Wedding = Wedding;
exports.validateWedding = validateWedding;
exports.validateGuest = validateGuest;
Here is my Router, or at least the important parts:
const auth = require('../middleware/auth');
const {Wedding, validateWedding, validateGuest, guestSchema} = require('../models/wedding');
const {User} = require('../models/user');
const _ = require('lodash');
const mongoose = require('mongoose');
const express = require('express');
const router = express.Router();
router.get('/:id/guests', auth, async (req, res) => {
const weddings = await Wedding.findById(req.params.id);
if(weddings.user_id != req.user._id)
res.status(400).send('This is not your wedding');
res.send(weddings.guest);
});
router.post('/:id/guests', auth, async (req, res) => {
const { error } = validateGuest(req.body);
if (error) return res.status(400).send(error.details[0].message);
const weddings_1 = await Wedding.findById(req.params.id);
if(weddings_1.user_id != req.user._id)
res.status(400).send('This is not your wedding');
const guest = mongoose.model('Guest',guestSchema);
guest.name = req.body.name;
guest.surname = req.body.surname;
guest.plz = req.body.plz;
let weddings = await Wedding.findByIdAndUpdate(req.params.id,{
guest: [ {
name : req.body.name,
surname: req.body.surname,
plz: req.body.plz
} ]
});
// weddings.guest.push(guest);
res.send(weddings);
});
module.exports = router;
I have tried to push the data into the DB and I have tried to update the whole document.
If anyone has any suggestions, thanks!
I think the problem is that you are not creating the new guest and not saving the Wedding either:
(thanks #ykit9 for the correction)
const Guest = mongoose.model('Guest', guestSchema);
const newGuest = new Guest({
name : req.body.name,
surname: req.body.surname,
plz: req.body.plz
});
newGuest.save();
Wedding.findOne({_id: req.params.id}, (err, foundWedding)=>{
foundWedding.guest.push(newGuest);
foundWedding.save();
res.send(foundWedding);
});
If you need further information: Constructing Documents in Mongoose - Documentation

Populate() Mongoose is not returning joined data

Well, I´am trying to retrieve Name and Lastname from user who created the document but it´s not working, it still returning Mongo´s Id
This is my areas model
var mongo = require('mongoose'),
validator = require('mongoose-unique-validator'),
Schema = mongo.Schema
var model = new Schema({
NAME: { type: String, required: true, unique: true, max: 50, min: 3 },
STATUS: { type: String, default: 'active' },
ADDED_BY: { type: Schema.Types.ObjectId, ref: 'users' },
ADDED_DATE: { type: Date, default: Date.now }
}, {collection :'areas'})
model.plugin( validator, { message: 'The {PATH} is not valid or duplicated' } )
module.exports = mongo.model('Area', model )
This is the user model
var mongo = require('mongoose'),
validator = require('mongoose-unique-validator'),
Schema = mongo.Schema
var userSchema = new Schema({
PERSONAL_DATA: {
NAME: { type: String, required: [ true, 'The name is necessary' ], max: 50 },
LAST_NAME: { type: String, required: [ true, 'the lastname is necessary' ], max: 100 },
PHOTO: { type: String, max: 100 },
BIRTHDAY: { type: Date },
MARITIAL_STATUS: { type: Schema.Types.ObjectId, ref: 'maritial_statuses' },
GENDER: { type: String, max: 1 },
EMAIL: { type: String, required: true },
},
COMPANY_DATA: {
JOB: { type: Schema.Types.ObjectId, ref: 'jobs' },
AREA: { type: Schema.Types.ObjectId, ref: 'areas' },
ROLE: { type: Schema.Types.ObjectId, ref: 'roles' },
BOSS: { type: Schema.Types.ObjectId, ref: 'users' },
}
}, { collection: 'users' } )
model.plugin( validator, { message: 'The {PATH} is not valid or duplicated' } )
module.exports = mongo.model('User', userSchema )
And this is my areas route
var express = require('express'),
model = require('../../models/catalogs/areas'),
app = express()
app.get('/:from', (req, res) => {
var from = parseInt( req.params.from )
model.find()
.sort('NAME').populate({ path: 'users', select: 'NAME LAST_NAME'})
.limit(10).skip(from)
.exec((error, data) => {
if (error) {
return res.status(500).json({
success: false,
error
})
}
res.status(200).json({
success: true,
data
})
})
})
module.exports = app
The response is
{
"success": true,
"data": [
{
"STATUS": "active",
"_id": "5c547f4adadf433914f72c8c",
"NAME": "Contabilidad y Finanzas",
"ADDED_BY": "5c4f562deec6f4defeea759b",
"ADDED_DATE": "2019-02-01T17:18:02.680Z",
"__v": 0
},
{
"STATUS": "active",
"_id": "5c547f3edadf433914f72c8b",
"NAME": "Tecnologías",
"ADDED_BY": "5c4f562deec6f4defeea759b",
"ADDED_DATE": "2019-02-01T17:17:50.579Z",
"__v": 0
}
]
}
As you seen, ADDED_BY is a field joined to Users and I want to retrieve that information. I don´t know what is wrong with my code.

Resources