Why use mongoose schema in Node.js - node.js

I was learning Node.js and Mongoose. Then, I cam across the term schema which is defined as the description of the structure of the data, default values and validation. So, as we know schema looks basically like this:
var TaskSchema = new Schema({
name: String,
priority: Number
});
The question I would like to ask is Why should we care about the description of structure of data that the schema allows to achieve, I mean, what is the main point in the use of schemas in mongoose? if you say, validation, then can't we achieve that with express-validator package? or...

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
See the documentation for more information.
Schemas not only define the structure of your document and casting of properties, they also define document instance methods, static Model methods, compound indexes, and document lifecycle hooks called middleware.
This is what allows you to communicate with the database easily using mongoose. It's not just for validation.

Related

How to create new Schema same as an existing one in mongoDB

I have a question in mongoDB. I have a user schema with different fields and methods. But I want to when deleting a user from this schema to place it in another schema for example deleted-users schema.
Should I create the deleted-users schema with the same exact fields but different name ?
Or is there a better way to this

How to retrieve schema from a mongoose collection?

using this line :
var collection = mongoose.connection.db.collection("users");
I get users s collection, now i need to get the schema from that collection ?
Is there a way to do this? thank you.
PS: I dont have any models
if you do not have any models, I'm afraid this can't be done.
the schema is the structural representation of the document, not the collection. You can use the schema (via the model) to enforce that any document added to the collection match some structure, but there's nothing that prevents you from inserting into this collection a different document without this model, that will not match the criteria
so the schema does not live in the DB, it is a part of the mongoose module, and lives in the code. and the way to access it is through the model only

MongoDB, Mongoose - what is the best way for adding non-persistent data field to the schema?

I have Mongoose schema and I need to add a non-persistent field to it. The point of this field is to store some status, related to persistent data fields, but without need to store it to the database. I see that some Mongoose alternatives provide such a feature e.g. https://github.com/simpleviewinc/mongolayer#modeladdfieldargs, however I am not able to find similar one inside Mongoose.
Any tip would be greatly appreciated!
In mongoose you have the concept of virtual fields. See doc (http://mongoosejs.com/docs/guide.html#virtuals).
See also related topic at Adding 'virtual' variables to a mongoose schema?

Mongoose - create model from existing db data

Using Mongoose, I have a document that was previously pulled from the database, complete with an _id property, in raw Object format (IE, without all of the document methods, just straight from the db).
How can I use that data to create an instance of mongoose.Model without the system assigning the model a new _id? I want to then eventually save that model and have it update the existing document in the database.
Update: using a combination of #Jack Newcombe's method, and subsequently setting model.isNew to false, I get the following error: "Mod on _id not allowed". So now it knows to update, but Mongoose is not removing the _id field from the update request. There has to be one more system property on the Model that tells Mongoose whether or not to remove the _id during an update request. Any ideas?
I've figured it out, but I'm sure there is a better way. I'm surprised there isn't a way to do this easily with Mongoose's API.
Anyway, you need to the following:
Create the model like this: var model = new Model(data,
{_id:false});
Manually set model.isNew to false
Manually tell Mongoose that the _id field hasn't been modified, like this: delete model.$__.activePaths.states.modify._id
The reason I'm not so fond of this is because I'm taking advantage of the fact that JavaScript doesn't have true protected methods, and I'm basically hacking Mongoose in order to get it to work. So I'd love to hear other answers if anyone has.
You can prevent the schema for the model from automatically generating an _id by passing in an option that sets the _id to false:
var schema = new Schema({ name: String }, { _id: false });
Source: http://mongoosejs.com/docs/guide.html#_id

Mongoose create custom _id from other fields

I know that Mongoose populates the _id field automatically with an ObjectID if none is given and that you can overwrite the _id when constructing and instance of the model.
What I want: create the _id from other fields in a transparent way. I want to omit the _id field when creating an instance of the model and then have a function called which fills it. This function should be declared on a Schema level and whoever uses the model does not know that _id was filled by the function instead of Mongoose.
Is there a hook or a parameter of the Schema constructor I missed?
Mongoose 3.0.x
Let's make this more concrete. Imagine a BlogPost and I want to create nice URLs by slugging the title. In order to map the slug to a Mongo Object I hash the slug and turn it into a ObjectID to leverage it's benefits. Now what I'm looking for is a transparent method which allows me to create an instance of BlogPost by only passing in title and have the slug and _id property automatically generated.
use a setter on title which slugifies and idifies for you: https://gist.github.com/3658511
If you want to make sure your code is only executed once the object is created, check for this.isNew inside the setter.
Is this what you are looking for?
You could define a function to create the _id before the model is saved, as in:
http://mongoosejs.com/docs/middleware.html
If this middleware is called after Mongoose creates the _id by default (my guess is it's not), you could tell Mongoose to not create an _id, with the _id option.
http://mongoosejs.com/docs/guide.html#options

Resources