Erase created mongoose model or dynamically change its properties - node.js

In mongoose, I need to find a maximal property value of given model. I have tried dynamically created the mongoose model in a function as
var DynamicSchema=new Schema({id:Number},{collection:collection});
var DynamicModel=mongoose.model('DynamicModel',DynamicSchema);
but after second run of the function I get an error 'OverwriteModelError: Cannot overwrite DynamicModel model once compiled.'
So I tried to create a model in intitialization without setting the collection as
var DynamicSchema = new Schema({id:Number});
var DynamicModel=mongoose.model('DynamicModel',DynamicSchema);
and in a function, tried to set the property collection by
var DynamicModel=mongoose.model('DynamicModel');
DynamicModel.set({collection : collection});
But it does not work. Is there any solution for this situation?

Related

Mongoose find data from dynamic Collection

I am new to MongoDB & working on a MEAN application.
In the mongo database(I am using mongoose), the collections are adding dynamically from third party API like schoolList1,schoolList2,schoolList3,schoolList4,....
I am facing problem to find a solution to get data from collections, Like If a user sends the argument from FrontEnd to find data from schoolList3.
The find function should apply on that collection only & return the data.
I am unable to solve it that how should I get data without passing schema and did not get any other way.
Set collection name option for your schema from user's input:
var collectionName = 'schoolList3'; // set value from the input
var dataSchema = new Schema({/** your schema here **/}, { collection: collectionName });

Set collection name dynamically with Mongoose

I need to change the collection based on model data in mongoose. I am tapping into the pre save event:
mySchema.pre('save', function (next) {
this.schema.set('collection', getCollectionName(this.dt));
next();
});
But that seems to have no effect on the save destination. Those collections seemed to be set in the mongoose model already. Any ideas on where I might be able to set this after the model has been created?

MongoDB: Can I use a created ObjectId before saving my document

Using mongoose on node.js:
Can I access the _id field on a model before it has been saved to the database and be sure that the ID will not change?
For example, I would like to do the following:
var model = new mongoose.model('MyModel');
someOtherObject.myModelId = String(model._id);
// Some more code...
model.save(...);
In Mongoose, _id values must always be assigned client-side as the docs indicate that:
Mongoose forces the db option forceServerObjectId false and cannot be overridden.
So the model._id value you get in the first line of your code will not be changed unless you do it in your own code before calling model.save.

Update mongoose node js without defined schema

I’m developing an app in NodeJS with mongoose for mongodb.
For some reasons, I don't define the schema, I just insert into the mongo the whole object that came from another function.
The problem is, when I try to update one object, I can't update one of the fields that is not defined in the schema. I just can update one field that is defined.
For example
var caseSchema = new mongo.Schema({
dhists : String,
dVisible : {'type':'number', default:1}
});
And, to create the object
var DCase = new dcase(data, false);
And then I use the other fields
DCase.dhists = JSON.stringify(hists);
This, in my mongodb has like 20 fields (18 from my source and 2 from my defined schema).
When I try to update one of the 18 fields, it doesn't update. When I try to update one of the other 2 fields (that are defined in the schema) it works.
Is there a way to do this?

mongoose custom schema types

I read from mongoose documentation that it is possible to create custom schema types to be added to the ones already existing.
As suggested I tried to look into the mongoose-long example: https://github.com/aheckmann/mongoose-long
I need this in an application I am developing in which I have a profile that is a mongoose model. The profile has several fields such as name, surname and so on that are MultiValueField. A MultiValueField is on object with the following structure:
{
current : <string>
providers : <Array>
values : {
provider1 : value1,
provider2 : value2
}
}
The structure above has a list of allowed providers (ordered by priorities) from which the field's value can be retrieved. The current property keeps track of which provider is currently picked to use as value of the field. Finally the object values contains the values for each provider.
I have defined an object in node.js, whose constructor takes the structure above as argument and provides a lot of useful methods to set a new providers, adding values and so on.
Each field of my profile should be initialized with a different list of providers, but I haven't found a way yet to do so.
If I define MultiValueField as an Embedded Schema, I can do so only having each field as Array. Also I cannot initialize every field at the moment a profile is created with the list of providers.
I think the best solution is to define a SchemaType MultiValueFieldType, that has a cast function that returns a MultiValueField object. However, how can I define such a custom schema type? And how can I use custom options when defining it in the schema of the profile?
I already posted a question on mongoose Google group to ask how to create Custom Schema Types: https://groups.google.com/forum/?fromgroups#!topic/mongoose-orm/fCpxtKSXFKc
I was able to create custom schema types only as arrays too. You will be able to initialize the model, but this aproach has drawbacks:
You will have arrays even if you don't need them
If you use ParameterSchema on other model, you will need to replicate it
Custom Schema Type Code:
// Location has inputs and outputs, which are Parameters
// file at /models/Location.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ParameterSchema = new Schema({
name: String,
type: String,
value: String,
});
var LocationSchema = new Schema({
inputs: [ParameterSchema],
outputs: [ParameterSchema],
});
module.exports = mongoose.model('Location', LocationSchema);
To initialize the model:
// file at /routes/locations.js
var Location = require('../models/Location');
var location = { ... } // javascript object usual initialization
location = new Location(location);
location.save();
When this code runs, it will save the initialized location with its parameters.
I didn't understand all topics of your question(s), but I hope that this answer helps you.

Resources