I'm trying to define the following mongoose model for a case. Strange I can not see a proper documentation about what I'm trying to do. In the end I want to have only 1 collection "Case"
which documents are nested object like this:
const CaseSchema = new Schema({
clientDataType: {
type: String,
required: true,
enum: [
'dataSchema1',
'dataSchema2',
],
},
clientData: {
type: "Either dataSchema1 or dataSchema2",
required: true,
},
caseDataType: {
type: String,
required: true,
enum: ['type1', 'type2', ...]
},
caseData: {
type: "One of 6 types of cases again they are schemas",
required: true,
}
}, { timestamps: true });
And here is mockup for dataSchema1 same goes for dataSchema2
const dataSchema1 = new Schema({
field1: {
type: String,
required: true,
},
fiedl2: {
type: String,
required: true,
},
field3: {
type: "One of subschemas of my choice",
required: true,
},
...
}, { timestamps: true });
Is mongoose capable of doing that or I must skip mongoose for this kind of case model. Basically at the end I wan't to submit this object and let mongoose validate it for me.
Related
i have schema which is nested
so need to validate whole schema, like if it has an extra attributes then throw error
i have tried with strict: 'throw' but that work only for main attributes, not for nested attributes.
const { Schema } = mongoose;
const DatasourceSchema = new Schema({
id: {
type: String,
unique: true,
required: true,
},
display_name: {
type: String,
required: true,
maxlength: 125
},
contact_info: {
type: Schema({
website: {
type: String,
required: false
},
registrar_phone: { type: String },
registrar_email: { type: String },
addresses: [{
type: Schema({
addr1: { type: String, required: false },
addr2: { type: String, required: false },
country: {
name: { type: String, required: true },
a2_code: { type: String, required: true },
}
}),
required: false
}]
}),
required: true,
}
},
{
strict: 'throw',
useNestedStrict: true
});
what i need is if i add any extra KEY (attributes) in object or array at any level that will throw errors
I want to generate two different objectIds in mongoose schema. One for departureLocation and one for arrivalLocation and want to reference it later.
I imported the object id from mongoose like this
let id = new mongoose.Types.ObjectId()
now I want to generate two different object ids like I said above
const routeSchema = new mongoose.Schema(
{
location: {
departureLocation: {
name: {
ref: "Location",
type: String,
required: true,
},
//want to create new object id here,
subLocation: [String],
_id: {
type: String,
},
},
arrivalLocation: {
name: {
ref: "Location",
type: String,
required: true,
},
//want to create new object id here,
subLocation: [String],
_id: {
type: String,
},
},
},
duration: {
type: Number,
required: true,
},
busId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Bus",
required: true,
},
date: {
type: String,
required: true,
},
},
{
timestamps: true,
}
);
MongoDB will automatically create _id for an object in a schema. You do not need to create one. In this schema, there will be 4 automatically generated _ids one for location, one for departureLocation, one for arrivalLocation, and one for the overall schema.
I am using node.js express.js and mongoose to develop my back end, I have this mongoose schema where I want to add a field called NumberRecords for every stored document this field will have the length value of the array csvData, I looked around some similar question here and I found I can use this pre function which I did but I get nothing on my database no field called NumberRecords is being added.
How do I fix this?
const rowSchema = new mongoose.Schema({
SESSION_ID: {
type: String,
required: false,
},
CARD_NUMBER: {
type: String,
required: false,
},
TERMINAL_ID: {
type: String,
required: false,
},
EXTERNAL_STAN: {
type: String,
required: false,
},
})
const csvSchema = new mongoose.Schema(
{
fileName: { type: String, required: true },
csvData: { type: [rowSchema], required: true },
NumberRecords: { type: Number, required: true },
},
{
timestamps: true,
}
);
csvSchema.pre("validate", function (next) {
this.NumberRecords = this.csvData.length;
next();
});
module.exports = mongoose.model("csvRecords", csvSchema);
I would like to post some hard coded values along with user input(variables) every time.
args: [{ type: mongoose.Schema.Types.Mixed, required: true }] >>in this array i would like to pass some hard coded values along with user input variables.
Well my data to be posted looks like this.
{"file": "**<user input>**","name":"<user input>", "className": "com.izac.Parser.IO", "args": ["-i", "{\"enrichedKafkaOptions\": {\"checkpointLocation\": \"**<hard coded path always remains the same>**", \"kafka.bootstrap.servers\": \"localhost:9092\", \"topic\": \"demoEnriched\"}, \"rejectedKafkaOptions\": {\"checkpointLocation\": \"/Users/vipulrajan/Desktop/checkpoints/DemoRejected\", \"kafka.bootstrap.servers\": \"localhost:9092\", \"topic\": \"demoRejected\"} }","-s", "{\"master\":\"local[*]\", \"appName\":\"app1\", \"config\":{\"jvm.memory\":\"4g\"} }"]};
This is my schema,
const mongoose = require('mongoose');
const livy_schema = mongoose.Schema({
file: { type: String, required: true },
name: { type: String, required: true },
className: { type: String, required: true },
args: [{ type: mongoose.Schema.Types.Mixed, required: true }] //here i have constants to pass on to
});
const kafka_schema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: { type: String, required: true, unique: false },
config: { type: mongoose.Schema.Types.Mixed, required: true } //here i have constants to pass on to
});
const enrichedEventSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
projectId: { type: mongoose.Schema.Types.ObjectId, ref: 'Project', required: true },
name: { type: String, required: true, unique: true },
description: { type: String, required: false },
type: { type: String, enum: ["Enriched"], required: true },
format: { type: String, enum: ["JSON", "DELIMITED", "FixedWidth", "LOG"], required: true },
kafka: [kafka_schema],
livy: [livy_schema]
});
Original question Asynchronous Programming in node js to pass constants/predefined mandatory values through mongoose model .
I'm kind of in dilemma, like should i pass this hardcodeded values in router.post() method(if its possible, how should i code?) or in schema? please guide me in right direction.
Please excuse me if I am misunderstanding the question.
Since you are using mongoose schema you can have your field default be a function where you can initialize and add hardcoded values.
Something like this:
const livy_schema = mongoose.Schema({
file: {
type: String,
required: true
},
name: {
type: String,
required: true
},
className: {
type: String,
required: true
},
args: [{
type: mongoose.Schema.Types.Mixed,
required: true,
default: function() {
return { data: 'hardcoded!', info: 'hardcoded!' }
}
}] //here i have constants to pass on to
});
)
if the schema is in the right context I assume you can easily replace those strings with values being passed or swap the default function.
I have postSchema which references the tagsSchema.
var tagsSchem = new Schema({
name: {
type: String,
required: true
}
}, {
timestamps: true
});
// create a schema
var postsSchema = new Schema({
title: {
type: String,
required: true,
unique: true
},
mainImage: {
type: String
},
category: {
type: String,
required: true
},
body: {
type: String,
required: true
},
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
tags: [tagsSchem]
}, {
timestamps: true
});
One post can contain any no. of tags. So if a post has 3 tags then I want to get all the posts with those 3 tags without querying it multiple times. Is it possible?
When you perform find, you can use the $in option to find values that are in your array. For example:
posts.find({tags:{$in:{["tag1","tag2","tag3"]}}, function(err,data) {
... //Your code here
}
This will take all the posts that contains one of the three tags. It's important you have to pass an array in the $in option. This should work.