Mongoose find data from dynamic Collection - node.js

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 });

Related

Maintain a custom order/sort of documents in MongoDB

In my web app XY I'm showing a classic list/table of data (documents) to the user. While all the sort functions provided by MongoDB ( and Mongoose, i'm using Mongoose) are pretty clear to me, I'm not interested in sorting by date or alphabetical order. In my case it would be important to let the user maintain a custom sort as in manually drag/drop items around to set a specific order of the documents (e.g. putting favourites in top of the list ). The UI to do this is a no-brainer but how to actually save the order in the database, brain-freeze.
Problem : How would I go about saving such a custom order of documents ?
What I use : NodeJS / Express / Mongoose (MongoDB)
Ideas
So far I could think of 2 ideas on how to do this.
A : Having an additional key (e.g. orderKey) in the mongoose Schema. Big con : I would need to keep constantly updating all documents orderKeys. Also I would need some sort of auto-increment for new documents.
const mySch = new Schema({
orderKey : { type : Number }
});
B : Creating one Schema/Model only for sorting, with an Array including all documents _ids for example. The order of the elements within the array would be used as reference for the custom order of the documents. Whenever the order changes, this Array would be changed as well.
conts orderSch = new Schema({
orderArray : { type : Array }
});
mongoose.model('Order', orderSch);
/* const customOrder = new Order({
orderArray : [ _id1, _id2, _id3, _id10, _id7, .. ]
}); */
Any more ideas or best practises are highly appreciated !

How to pass variable name as collection name for mongo db in nodejs

I am using node js and mongo db and I want to pass a variable to collections
var id = "someid";
db.collection(id).insert("some json data");
If I did like this it is giving me an error as collection name must be a string.
You can create variable using var for collection name
var colName = "mytest"
and then execute all the operations on collections as below:
db[colName].find()
db[colName].rename("newName")
etc. This will help you keep your collection name dynamic and can even update it keeping your commands same.
Hope this helps!

Erase created mongoose model or dynamically change its properties

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?

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?

Resources