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"
Related
I have a database with values like this:
I also have a post method with the goal of updating the startBudget when a form is submitted.
All I want to do is update the startBudget number in the database. I tried doing that by using the updateOne function but it didn't change anything in the database. What am I doing wrong?
Figured it out. It turns out I needed to use save instead of updateOne. All good.
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.
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 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.
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