I'm making a note app, each note will have multiple categories.
var NotesSchema = new mongoose.Schema({
title: String,
note: String,
favorite: {type: Boolean, default: false},
category: [{ type: mongoose.Schema.Types.ObjectId, ref: "categories" }]
},{ timestamps: { createdAt: 'created_at' } });
var Notes = mongoose.model('Notes', NotesSchema);
var CategoriesSchema = new Schema({
name: {type: String, required: true},
favorite: {type: Boolean, default: false},
})
var Categories = mongoose.model('Categories', CategoriesSchema);
I can do this with only one category but I don't know how it's done for multiple categories.
First I thought I need to store the categories, get each id and then store the note. Here's what I tried:
.get('/', (req, res) => {
var data = {
title : "scientific revolution",
note : " some note",
favorite : true,
category : [ 'science', 'books']
}
var catIds = [], seriesIds = [];
data.category.map(cat =>{
const category = new Categories({
name: cat
})
category.save((err,data)=>{
if(err) throw err;
catIds.push(data._id)
})
})
data.category = catIds;
const note = new Notes(data)
note.save((err, data)=>{
if(err) throw err;
res.json(data)
})
})
The catIds array never gets the ids!
I'm totally new at using references. Didn't even know they existed.
Your ref field should point to the model name not the table name.
In this case Categories.
I see you have write {ref: 'categories'} with small c but mongoose collection name is case sensitive and you should do {ref: 'Categories'}.
You can read more about mongoose case sensitivity in this post
Related
This is my product Schema using mongoose in nodeJs. However I am developing a REST API.
const ImageSchema = new Schema({
path: {
type: String
},
pos: {
type: Number
}
});
const ProductSchema = new Schema({
title: {
type: String,
required: [true, 'Product title is required.']
},
description: {
type: String
},
created_date: {
type: Date ,
required: [true, 'Created Time required.'],
default: Date.now
},
images: [
ImageSchema
]
});
const Product = mongoose.model('product', ProductSchema);
module.exports = Product;
This is how I update a product
router.put('/:id', upload.single('pImg'), function(req, res, next){
var x = req.body;
Product.findByIdAndUpdate({
_id: req.params.id
}, x).then(function(){
Product.findOne({
_id: req.params.id
}).then(function(product){
res.send(product);
});
}).catch(next);
});
My question is how can I push into the images array and also update other fields like title, description at the same time ?
you can use $push and $set in your call.
I'm creating an app where you log workouts and I'm having some problems with Mongoose.
I have two schemas, one for workouts and one for exercises. When the user adds a new exercise, I want it to be stored inside the workout, and I've been trying this in a bunch of ways.
For now, the exercises are saved in a different collection in my MongoDB (don't know if this is the best way to do it), and I thought that it should save the exercise inside the workout.exercises, but there is only the objectID. How do I resolve this? Have looked at the populate function, but can't figure out how to get it to work.
addExercises
export function addExercise(req, res) {
if (!req.body.exercise.title) {
res.status(403).end();
}
const newExercise = new Exercise(req.body.exercise);
// Let's sanitize inputs
newExercise.title = sanitizeHtml(newExercise.title);
newExercise.cuid = cuid();
newExercise.sets = [];
newExercise.save((err, saved) => {
if (err) res.status(500).send(err);
});
Workout
.findOneAndUpdate(
{cuid: req.body.exercise.workoutCUID},
{$push: {exercises: newExercise}},
{upsert: true, new: true},
function (err, data) {
if (err) console.log(err);
});
}
getExercises
export function getExercises(req, res) {
Workout.findOne({cuid: req.params.cuid}).exec((err, workout) => {
if (err) {
res.status(500).send(err);
}
console.log(workout);
let exercises = workout.exercises;
res.json({exercises});
});
}
Workout
import mongoose from "mongoose";
const Schema = mongoose.Schema;
var Exercise = require('./exercise');
const workoutSchema = new Schema({
title: {type: 'String', required: true},
cuid: {type: 'String', required: true},
slug: {type: 'String', required: true},
userID: {type: 'String', required: true},
exercises: [{ type: Schema.Types.ObjectId, ref: 'Exercise' }],
date: {type: 'Date', default: Date.now, required: true},
});
export default mongoose.model('Workout', workoutSchema);
Exercise
import mongoose from "mongoose";
const Schema = mongoose.Schema;
var Workout = require('./workout');
const exerciseSchema = new Schema({
title: {type: 'String', required: true},
cuid: {type: 'String', required: true},
workoutCUID: {type: 'String', required: true},
sets: {type: 'Array', "default": [], required: true}
});
export default mongoose.model('Exercise', exerciseSchema);
Based on your Workout schema, you declare the type of the Exercises field to be [{ type: Schema.Types.ObjectId, ref: 'Exercise' }]. This means that this field should be an array of Mongoose ObjectId's.
It appears that you are attempting to add the whole exercise object to the workout's exercises field, rather than just the ObjectId. Try modifying it this way:
const newExercise = new Exercise(req.body.exercise);
// Let's sanitize inputs
newExercise.title = sanitizeHtml(newExercise.title);
newExercise.cuid = cuid();
newExercise.sets = [];
newExercise.save((err, saved) => {
if (err) res.status(500).send(err);
// Nest the Workout update in here to ensure that the new exercise saved correctly before proceeding
Workout
.findOneAndUpdate(
{cuid: req.body.exercise.workoutCUID},
// push just the _id, not the whole object
{$push: {exercises: newExercise._id}},
{upsert: true, new: true},
function (err, data) {
if (err) console.log(err);
});
});
Now that you correctly have the ObjectId saved in the exercises field, .populate should work when you query the workout:
Workout.findById(id).populate("exercises").exec((err, workout) => {
// handle error and do stuff with the workout
})
Workout.findById(req.params.id).populate("exercises").exec((err,workout) =>{
res.status(200).json(workout);
})
It should work this way
I have a simple relation between topics and categories when topic belongs to a category.
So schema looks like this:
const CategorySchema = new mongoose.Schema({
name: String,
slug: String,
description: String
});
And topic
const TopicSchema = new mongoose.Schema({
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Category'
},
title: String,
slug: String,
body: String,
created: {type: Date, default: Date.now}
});
I want to implement particular embedding of category into topic
{
category: {
_id: ObjectId('abc'),
slug: 'catslug'
},
title: "Title",
slug: "topictitle",
...
}
It will help me avoid unnecessary population and obtain performance bonuses.
I don't want to embed whole document because I want to changes categories sometimes (it is a rare operation) and maintain references.
Hope this helps, done it in my own project to save some RTTs in common use cases. Make sure you're taking care of both copies on update.
parent.model.js:
const mongoose = require('mongoose');
const childEmbeddedSchema = new mongoose.Schema({
_id: {type: mongoose.Schema.Types.ObjectId, ref: 'Child', auto: false, required: true, index: true},
someFieldIWantEmbedded: {type: String}
});
const parentSchema = new mongoose.Schema({
child: { type: childEmbeddedSchema },
moreChildren: { type: [{type: childEmbeddedSchema }] }
});
module.exports = mongoose.model('Parent', parentSchema);
child.model.js:
const mongoose = require('mongoose');
const childSchema = new mongoose.Schema({
someFieldIWantEmbedded: {type: String},
someFieldIDontWantEmbedded: {type: Number},
anotherFieldIDontWantEmbedded: {type: Date}
});
module.exports = mongoose.model('Child', childSchema);
parent.controller.js:
const mongoose = require('mongoose');
const Parent = require('path/to/parent.model');
exports.getAll = (req, res, next) => {
const query = Parent.find();
// only populate if requested! if true, will replace entire sub-document with fetched one.
if (req.headers.populate === 'true') {
query.populate({
path: 'child._id',
select: `someFieldIWantEmbedded ${req.headers.select}`
});
query.populate({
path: 'moreChildren._id',
select: `someFieldIWantEmbedded ${req.headers.select}`
});
}
query.exec((err, results) => {
if (err) {
next(err);
} else {
res.status(200).json(results);
}
});
};
Here are two collections' schema
var activitySchema = new Schema({
activity_id: {type: String, index: {unique: true}, required: true},
begin_date : String,
...
})
var registrationSchema = new Schema({
activity_id: {type: String, index: {unique: true}, required: true},
registration_id: {type:String, trim: true, index: true ,required: true },
email : String,
...
})
I want activity.begin_date , registration.id , registration.email in the same query. How can I do? I've found some solutions on the internet, but still don't know whether using populate or aggregation $lookup (this one seems new).
Here's how I tried, but not working at all.
models.Registration.aggregate([
{
$lookup: {
from: "Activity",
localField: "activity_id",
foreignField: "activity_id",
as: "activity_docs"
}
},
{"$unwind" : "activity_docs"},
], function( err , result ){
if(result){
fullDoc = result;
}else{
next( err );
}
})
activity_id should be ObjectId data type. ObjectId documentation
If you want to use pupoluate, you must to use ref to the other schema.
Population documentation
var activitySchema = new Schema({
begin_date : String,
...
})
var Activity= mongoose.model('Activity', activitySchema );
var registrationSchema = new Schema({
activity_id: {type: Schema.Types.ObjectId, ref : 'Activity', required: true},
email : String,
...
})
var Registration = mongoose.model('Registration', registrationSchema);
So the query such as :
var query = Registration.find({_id: 'registration_id parameter'});
query.select('_id activity_id email');
query.populate('activity_id','_id begin_date');
query.exec(function(error,result){
if(error){
/// handle error
}else{
// handle result
}
});
I have a model called Shop whos schema looks like this:
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ShopSchema = new Schema({
name: { type: String, required: true },
address: { type: String, required: true },
description: String,
stock: { type: Number, default: 100 },
latitude: { type: Number, required: true },
longitude: { type: Number, required: true },
image: String,
link: String,
tags: [{ type: Schema.ObjectId, ref: 'Tag' }],
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now }
});
module.exports = mongoose.model('Shop', ShopSchema);
I want to use the array tags to reference to another model via ObjectId obviously. This set up works fine when I add ids into the property via db.shops.update({...}, {$set: {tags: ...}}) and the ids get set properly. But when I try to do it via the Express.js controller assigned to the model, nothing gets updated and there even is no error message. Here is update function in the controller:
// Updates an existing shop in the DB.
exports.update = function(req, res) {
if(req.body._id) { delete req.body._id; }
Shop.findById(req.params.id, function (err, shop) {
if (err) { return handleError(res, err); }
if(!shop) { return res.send(404); }
var updated = _.merge(shop, req.body);
shop.updatedAt = new Date();
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, shop);
});
});
};
This works perfect for any other properties of the Shop model but just not for the tags. I also tried to set the type of the tags to string, but that didn't help.
I guess I am missing something about saving arrays in Mongoose?
It looks like the issue is _.merge() cannot handle merging arrays properly, which is the tags array in your case. A workaround would be adding explicit assignment of tags array after the merge, if it is ok to overwrite the existing tags.
var updated = _.merge(shop, req.body);
if (req.body.tags) {
updated.tags = req.body.tags;
}
Hope this helps.. If the workaround is not sufficient you may visit lodash forums.