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.
Related
I'm using MongoDB v3.6.3 with PyMongo.
Here's my document structure:
{
"process_id": number,
"created_dttm": date,
"updated_dttm": date
}
I want to do two things:
Whenever a new document is inserted, created_dttm and updated_dttm should have the current system date.
Whenever an existing document is updated, updated_dttm should be updated to the current system date at that time
I have done this using MongoEngine Models by overriding the save() and update() methods .
Is there any other way to do this using PyMongo other than explicitly handling this programatically while insert/update?
Unfortunately this doesn't come out of the box from mongodb/pymongo. The only thing you get is if you use ObjectId's as primary keys for your documents, you can extract the timestamp from it with
oid = ObjectId()
oid.generation_time # is a datetime.datetime
For the update timestamps, you'll need to handle that in your application code. There is usually 2 ways for doing this, either you emit & store audit events in a separate collection, either you wrap your update method and modify a last_update_timestamp every time it is called.
I'm using mongoose. but I want to get all documents that submitted at a specific time. I have a time field like this:
"time": "10:11:1",
How can I do ?
You can use js native function Date.toLocaleTimeString():
this will display like this
"08:00:12"
I am referring to this Time to live in mongodb, mongoose dont work. Documents doesnt get deleted to ask my question:
Is it possible to set TTL time for MongoDB dynamically?
So let's suppose you have token collection and you want to use it for different purposes. In that case every time when you create the token it would be nice to set specific TTL for each token.
If this is possible, could you please provide some code snippet?
To dynamically set a TTL to a document you can make use of the same index, but create another field like expireAt in the schema like:
expireAt: {
type: Date,
default: null,
}
Then create an index like (example for mongoose):
schema.index({ expireAt: 1 }, { expireAfterSeconds: 0 });
Now for all documents that you want to expire, you can set the exact datetime. For others, whose field expireAt is defaulting to null won't expire.
You can see the same example here in MongoDB Docs.
If you define a TTL index on a collection, then periodically MongoDB will remove() old documents from the collection.
db.events.ensureIndex('time', expireAfterSeconds=3600)
It use an indexing system for handling TTL. Its fixed, there is no way to define it dynamically for each document. in your scenario I recommend you to use Messaging System like RabbitMQ along with MongoDB
https://www.rabbitmq.com/ttl.html
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?
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.