Reputation: 7975
Intending to log use of the API (user/route/params/time) in a Heroku/Node/Express/Mongodb web app, to allow various analytics (who/what/when/how often). One way I can think of is to push those to MongoDB.
Mongo will generate an ID automatically, and I see that it's possible to extract the created time from the autogenerated ID, but since the time stamp is all I want, now I wonder if I can use the date as the ID?
This seems to work, and the timestamps seem granular enough ("_id" : ISODate("2012-11-30T21:18:24.484Z")
) that they'll be unique. Is this okay or just asking for an "ID not unique" error just when things get going?
var apilogSchema = new mongoose.Schema({
_id: {type: Date, default: Date.now},
userId: {type: mongoose.Schema.Types.ObjectId, required: false},
route: {type: String, required: false}
})
Get date and time from mongodb document _id field
Upvotes: 0
Views: 948
Reputation: 14434
what johnny said, wouldnt recommend that. especially when you are using it for logging user actions. (it is possible that 2 users do an action at the same millisecond, and now your id isnt unique anymore)
check out node-uuid if you are on node.js and want your id to contain a timestamp
Upvotes: 1