Is it possible to have a "Time" Schema in Mongoose? - node.js

I know I can do Date. but what about Time?

Are you just trying to get created at/updated at timestamps? If so you can use mongoose-types to add that as a plugin. Or you could use a mixed type I suppose.

Related

set only time in mongoose schema

How to set only time in mongoose schema ?
like we can do in sequelize schema
startTime: DataTypes.TIME,
endTime: DataTypes.TIME
is their any way in mongoose to set time in mongoose schema??
It's simple Date and nothing else like this --> startTime : Date
Here is the documentation for all available data types for mongoose: http://mongoosejs.com/docs/schematypes.html
In case you only require the time of the date you can use a getters to achieve that: http://mongoosejs.com/docs/2.7.x/docs/getters-setters.html
I've tried achieving the same thing before but failed.
The best solution that was I able to come up with was to just add the time during the insert to the database and not when creating the schema.
This way you can use datetime.toLocaleTimeString(); or moment.js to just add the time without the date etc.
I'm afraid there is no mongoose schema type to store a time like you can do in SQL.
I have stored a time as a Date in one of my project, keeping just the time part of that date. It works more or less.
And maybe MongoDB $hour and $minute aggregation can help.
But it is not a very elegant solution.
Another possibility is to store your time as separate Number (hours, minutes, seconds).
Best method I found so far is to store it as a String, as mentioned in this answer.
Maybe you can go further into this idea and create your own custom schema Time type.

How can I make mongoose to validate an entire 'outdated' MongoDB collection?

during the development of my app, i very often add custom and new fields to an existing schema, making the 'old' content in my mongodb 'incompelete' and missing the new fields. this leads sometimes to null content where it's required and it's in my use case very bad.
my question is what command/utility do i need to use to make mongoose validate my old documents, and in potential add those missing fields with pre-defined defaults to the old documents?
i remember reading something about that kind of functionality when i started learning how to use mongoose, but i just can't find it anywhere anymore..
thanks in advance :)
Amit

Autoincrement id field in MongoDB and nodejs(express framework)

How can i set an "id"-field in MongoDB-database with autoIncrement. I need unique numeric id.
I know, that there's simple method instead of counting items...
my application made with nodejs and expressjs framework
I have used mongoose auto increment plugin of mongoose for doing the same. It is quite helpful. You should have a look and give it a try.

Get documents with field that isn't a particular string

I want to get the documents from MongoDB that don't have the field "name":"John" (just an example).
How can I do this?
For getting the documents that have the field "name":"John" I use:
db.col.find({"name":"John"})
I've already tried ...find(!{"name":"John"}).
db.col.find({"name":{$ne: "John"}})
should work, here are docs: Mongo, Advanced Queries.
Just use $ne selector like this:
db.col.find({"name":{$ne:"John"}})
http://docs.mongodb.org/manual/reference/operator/ne/#_S_ne

Adding default values to mongoose models at runtime

I want to have default values for certain fields in my mongoose models. The trick is I don't want to store these values in the database, but what to add them when the model is initialized.
Please help.
You can define an 'init' middleware function for the schema which runs when a model instance is loaded from the database. That should let you manipulate the instance to add your defaults as needed.
Also see this related question for more details as the docs are pretty spare on this.
Maybe a json file?
Using a require('path/to/json' ) you can acces to it and get the values.
Im actually doing something like that, but using the same JSON SCHEMA wich is build for validation purpose.

Resources