Mongoose Schema registration - passing config info - javascript

What's a good way to pass configuration information while registering mongoose schemas and using them?
Say in the below example, I want to make the read preference and timeout value come from a config file... "read: ['nearest']}, safe: {wtimeout: 10000})"
something like => "read: [dbConfig.readPrefence]}, safe: {wtimeout: dbConfig.writeTimoutMS})", what's a good way to pass dbConfig?
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var blogSchema = new Schema({
title: String,
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
}, {read: ['nearest']}, safe: {wtimeout: 10000}));

The solution may depend on how you've organized your files, meaning what you might be asking is how to pass around a configuration to multiple files which each contain a schema. But first looking at the simple case, a single schema exists within this file, I would recommend loading the configuration from there and pass in the values.
config.json
{
"read": "nearest",
"safe": true
}
my-schema.js
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
config = require('./config.json');
var blogSchema = new Schema({
title: String,
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
}, {
read: config.read,
safe: config.safe
});
If you have multiple schemas and you'd rather load the configuration only once and pass that around, then I'd recommend sending it within the require call of the schema file. For example:
app.js
var mySchema = require("./my-schema.js")(config);
my-schema.js
module.exports = function(config) { ... }

Related

Add a new field to every document from my mongo DB collection via mongoose

This question was asked several times, but despite that, I wasn't able to solve my problem. In my mongoose collection, I store 30 users with the following mongoose schema. I want to implement a newsletter on my site, therefore I want to add the new field:
newsletter: {
type: Boolean,
default: true
},
My question is: How can I add newsletter false/true to every user?
I found that, but it didn't work.
User.updateMany({}, [{ $set: { newsletter: false }}])
My Schema:
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
name: { type: String, required: true },
email: { type: String, required: true },
password: { type: String, required: true },
date: { type: Date, default: Date.now },
token: { type: String, required: true }
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
Adding to the schema "newsletter" does solve the problem for new users, but doesn't add the field to the already existing ones.

Mongoose Subdocument in another Invalid Schema error

I have 2 separate files, one encapsulates the Slot Schema, and another for the Location Schema. I'm trying to have a field in the Slot Schema that references the location Schema.
const mongoose = require('mongoose')
const locationSchema = require('./location')
const slotSchema = mongoose.Schema({
time: {
required: true,
type: String
},
typeOfSlot:{
required: true,
type: String
},
academic_mem_id:{
required: true,
default: null,
type: Number
},
course_id:{
required: true,
type: Number
},
location: [ locationSchema] // adjust
});
module.exports = mongoose.model('slots', slotSchema)
In a separate File:
const mongoose = require('mongoose')
const locationSchema = mongoose.Schema({
name:{
type:String,
required: true
},
capacity:{
type: Number,
required: true
},
type:{
type:String,
required:true
}
});
module.exports = mongoose.model('location', locationSchema)
I get this error when I run :
throw new TypeError('Invalid schema configuration: ' +
^
TypeError: Invalid schema configuration: `model` is not a valid type within the array `location`.
I'd really appreciate it if you'd help me figure out why the code above is wrong.
I want to export both the model and the Schema.
You are not exporting the locationSchema, but the location model. That is something entirely different and that is the reason you get the model is not a valid type within the array error.
Export only the schema and create/export the model in a separate file, e.g. locationModel.
const mongoose = require('mongoose')
const { Schema } = mongoose;
const locationSchema = new Schema({
name:{
type:String,
required: true
},
capacity:{
type: Number,
required: true
},
type:{
type:String,
required:true
}
});
module.exports = locationSchema;
Or if you want to keep both in the same file and export both:
module.exports = {
locationSchema,
locationModel,
};
And import them like so:
const { locationSchema, locationModel } = require('path/to/location.js');
That's wrong way referencing to other models.
First, you don't need require locationSchema, you can refer to that module in Schema. In your Slot Schema write this instead of your location field
location: {
type: mongoose.Schema.ObjectId,
ref: "location"
}

Adding JSON array to a Mongoose schema (JavaScript)

I'm creating an Android App (A baseball app) where I'm using MongoDB to store my data. the way I want my JSON data to be stored into the database is like this.
{"email#domain.com":{
"coachName": "Smith"
players:[
player1:{
"throws_":"n\/a",
"position":"position not set",
"number":"1",
"playerNum":"H8E83NxRo6",
"bats":"n\/a",
"team_name":"Team",
"name":"Name"}
player2:{
"throws_":"n\/a",
"position":"position not set",
"number":"1",
"playerNum":"H8E83NxRo6",
"bats":"n\/a",
"team_name":"Team",
"name":"Name"}
]
}
sorry if there is any syntax error, but essentially that is the layout i want for the JSON. Where the mongo page "id" is the persons email. and where "players" is an array of the list of players the coach has.
My question is, how can I
properly setup the Mongoose schema to look like this?
when the app sends the JSON data, how can I parse it to store the data?
and if possible (ill try and figure this part on my own if no one can) if multiple players are being added at once, how can i store them if there's already players in the array?
All of this is backend/server side, I have the android working properly, i just need help with storing it to look like this in JavaScript.
You dont want to use a dynamic variable as a field name. I'm talking about the email you have "email#domain.com". This wouldn't be good because how will you find the object. Its not common to search for object by there fields, you use the field name to describe what it is your looking for. You will need 2 models. One for player and one for coach. Coach refrences a Player in its Players array field.
If you format your JSON correctly you wont have to do any parsing, just make sure the JSON you are sending is correctly formatted.
Look at the addPlayer function.
Controller file (Answer for questions 2 and 3)
create = function(req, res) {
var coach = new Coach(req.body);
coach.user = req.user;
coach.save(function(err) {
if (err) {
return res.status(400).send({
// Put your error message here
});
} else {
res.json(coach);
}
});
};
read = function(req, res) {
res.json(req.coach);
};
addPlayer = function(req, res) {
var coach = req.coach;
console.log('updating coach', req.coach);
var player = new Player(req.newPlayer);
coach.players.push(newPlayer);
coach.save(function(err) {
if (err) {
return res.status(400).send({
// Put your error message here
});
} else {
res.json(coach);
}
});
};
Player
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Player Schema
*/
var PlayerSchema = new Schema({
created: {
type: Date,
default: Date.now
},
throws: {
type: Number,
},
name: {
type: String,
default: '',
trim: true
},
position: {
type: String,
default: '',
trim: true
},
playerNum: {
type: String,
default: '',
trim: true
},
position: {
type: Number,
default: 0,
trim: true
},
teamName: {
type: String,
default: '',
trim: true
}
});
mongoose.model('Player', PlayerSchema);
Coach Schema
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Coach Schema
*/
var CoachSchema = new Schema({
created: {
type: Date,
default: Date.now
},
email: {
type: String,
unique: true,
lowercase: true,
trim: true,
default: ''
},
name: {
type: String,
default: '',
trim: true
},
players: [{
type: Schema.ObjectId,
ref: 'Player'
}
});
mongoose.model('Coach', CoachSchema);

Inserting Object ID's into array in existing Mongoose schema with Node.js

I have an existing News articles section that I want to add categories to for more refined searching, my Schema's are as follows:
var ArticleSchema = new Schema({
title: String,
body: String,
author: {
type: Schema.Type.ObjectId,
ref: 'User',
required: true
},
image: String,
catagories: [{
type: Schema.Types.ObjectId, ref: 'Catagory'
}],
meta: {
created: {
type: Date,
'default': Date.now,
set: function(val) {
return undefined;
}
},
updated: {
type: Date,
'default': Date.now
}
}
});
ArticleSchema.statics.search = function (str, callback) {
var regexp = new RegExp(str, 'i');
return this.find({'$or': [{title: regexp}, {body: regexp}]}, callback);
};
module.exports = ArticleSchema;
var CatagorySchema = new mongoose.Schema({
name: { type: String, unique: true },
});
module.exports = CatagorySchema;
I want a user friendly input for selecting categories (don't even know what is best here, be it check-box's or a simple comma separated text input etc.). My question is what is the best practice for obtaining this kind of input and translating that into the Article Schema (providing the categories exist). If anyone could point me in the right direction it would be much appreciated. Thanks.
Keep the category names you want to search for in an array
{
categories: ["cat1", "cat2"]
}
then you can add an index to it and do a $in query. the current schema is not very good because you cannot look for the category in a single query but need to resolve all the "categories" links first.

Whats the reason for not being able to nest Schemas without an array

I was just wondering why something like this isn't allowed in mongoose schema definitions:
var NameSchema = new mongoose.Schema({
first: {type: String, trim: true },
last: {type: String, trim: true }
});
var UserSchema = new mongoose.Schema({
name: NameSchema, // this line causes an error
age: {type: Number}
});
It seems like a design decision, I was just wondering if I could get an explanation as to why it isn't supported
Thanks!
You can nest a schema using this method:
name: [{ some: "props" } ]
or
name: [NameSchema]
The problem with giving directly the schema definition (without using "type: ...") is that Mongoose can't make the difference between the option object and the schema object. Mongoose will think that NameSchema is actually an option object (containing options such as the type, trim...).

Categories

Resources