How to query mongodb with mongoose + adding created date in results
up vote
1
down vote
favorite
I want to add the creation date to results returned by mongoose. I know the _id property of a mongodb record has the creation date embedded in it, but I'm not sure how to add it to the result set so that I can use it in my client side application.
Any ideas on how to add that property dynamically to the results set?
mongoose
add a comment |
up vote
1
down vote
favorite
I want to add the creation date to results returned by mongoose. I know the _id property of a mongodb record has the creation date embedded in it, but I'm not sure how to add it to the result set so that I can use it in my client side application.
Any ideas on how to add that property dynamically to the results set?
mongoose
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I want to add the creation date to results returned by mongoose. I know the _id property of a mongodb record has the creation date embedded in it, but I'm not sure how to add it to the result set so that I can use it in my client side application.
Any ideas on how to add that property dynamically to the results set?
mongoose
I want to add the creation date to results returned by mongoose. I know the _id property of a mongodb record has the creation date embedded in it, but I'm not sure how to add it to the result set so that I can use it in my client side application.
Any ideas on how to add that property dynamically to the results set?
mongoose
mongoose
edited Aug 10 '14 at 14:15
Stennie
45.5k8108138
45.5k8108138
asked Aug 9 '14 at 9:36
Jorre
7,1472377126
7,1472377126
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
If you are willing to use _id's timestamp as the date created
var fooSchema = new Schema({
name: String,
...
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
}
)
fooSchema.virtual('created')
.get(function(){
return this._id.getTimestamp();
});
You can access the field created to get the timestamp.
The toObject(doc) and toJson(doc) options are used to keep our virtual field(created) when converting from a mongoose document. Using toJson might suffice I guess, depends on your usage.
If you are not willing to use _id's timestamp(there are many arguments that it is not advisable when operating across multiple instances), you can put it in a separate field like
var fooSchema = new Schema({
name: String,
created: {type: Date, default: Date.now},
});
This will set created to the timestamp the object is first saved.
add a comment |
up vote
0
down vote
I prefer the second way of ma08.Add a created field and use the default value to Date.now.This method automatically creates the current date as the created date.
var nSchema=new Schema({
name:String,
...
created: {type:Date, default:Date.now},
...
});
add a comment |
up vote
0
down vote
Mongoose can do it for you automatically. according to timestamp option in Mongoose just do something like this :
var nSchema=new Schema({
name:String,
...
timestamps: { createdAt: 'created_at' },
...
});
also you can track for update with updateAt
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If you are willing to use _id's timestamp as the date created
var fooSchema = new Schema({
name: String,
...
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
}
)
fooSchema.virtual('created')
.get(function(){
return this._id.getTimestamp();
});
You can access the field created to get the timestamp.
The toObject(doc) and toJson(doc) options are used to keep our virtual field(created) when converting from a mongoose document. Using toJson might suffice I guess, depends on your usage.
If you are not willing to use _id's timestamp(there are many arguments that it is not advisable when operating across multiple instances), you can put it in a separate field like
var fooSchema = new Schema({
name: String,
created: {type: Date, default: Date.now},
});
This will set created to the timestamp the object is first saved.
add a comment |
up vote
2
down vote
If you are willing to use _id's timestamp as the date created
var fooSchema = new Schema({
name: String,
...
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
}
)
fooSchema.virtual('created')
.get(function(){
return this._id.getTimestamp();
});
You can access the field created to get the timestamp.
The toObject(doc) and toJson(doc) options are used to keep our virtual field(created) when converting from a mongoose document. Using toJson might suffice I guess, depends on your usage.
If you are not willing to use _id's timestamp(there are many arguments that it is not advisable when operating across multiple instances), you can put it in a separate field like
var fooSchema = new Schema({
name: String,
created: {type: Date, default: Date.now},
});
This will set created to the timestamp the object is first saved.
add a comment |
up vote
2
down vote
up vote
2
down vote
If you are willing to use _id's timestamp as the date created
var fooSchema = new Schema({
name: String,
...
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
}
)
fooSchema.virtual('created')
.get(function(){
return this._id.getTimestamp();
});
You can access the field created to get the timestamp.
The toObject(doc) and toJson(doc) options are used to keep our virtual field(created) when converting from a mongoose document. Using toJson might suffice I guess, depends on your usage.
If you are not willing to use _id's timestamp(there are many arguments that it is not advisable when operating across multiple instances), you can put it in a separate field like
var fooSchema = new Schema({
name: String,
created: {type: Date, default: Date.now},
});
This will set created to the timestamp the object is first saved.
If you are willing to use _id's timestamp as the date created
var fooSchema = new Schema({
name: String,
...
},
{
toObject: { virtuals: true },
toJSON: { virtuals: true }
}
)
fooSchema.virtual('created')
.get(function(){
return this._id.getTimestamp();
});
You can access the field created to get the timestamp.
The toObject(doc) and toJson(doc) options are used to keep our virtual field(created) when converting from a mongoose document. Using toJson might suffice I guess, depends on your usage.
If you are not willing to use _id's timestamp(there are many arguments that it is not advisable when operating across multiple instances), you can put it in a separate field like
var fooSchema = new Schema({
name: String,
created: {type: Date, default: Date.now},
});
This will set created to the timestamp the object is first saved.
edited Aug 9 '14 at 11:45
answered Aug 9 '14 at 10:57
ma08
2,2941330
2,2941330
add a comment |
add a comment |
up vote
0
down vote
I prefer the second way of ma08.Add a created field and use the default value to Date.now.This method automatically creates the current date as the created date.
var nSchema=new Schema({
name:String,
...
created: {type:Date, default:Date.now},
...
});
add a comment |
up vote
0
down vote
I prefer the second way of ma08.Add a created field and use the default value to Date.now.This method automatically creates the current date as the created date.
var nSchema=new Schema({
name:String,
...
created: {type:Date, default:Date.now},
...
});
add a comment |
up vote
0
down vote
up vote
0
down vote
I prefer the second way of ma08.Add a created field and use the default value to Date.now.This method automatically creates the current date as the created date.
var nSchema=new Schema({
name:String,
...
created: {type:Date, default:Date.now},
...
});
I prefer the second way of ma08.Add a created field and use the default value to Date.now.This method automatically creates the current date as the created date.
var nSchema=new Schema({
name:String,
...
created: {type:Date, default:Date.now},
...
});
answered Aug 9 '14 at 12:04
SUNDARRAJAN K
1,60221434
1,60221434
add a comment |
add a comment |
up vote
0
down vote
Mongoose can do it for you automatically. according to timestamp option in Mongoose just do something like this :
var nSchema=new Schema({
name:String,
...
timestamps: { createdAt: 'created_at' },
...
});
also you can track for update with updateAt
add a comment |
up vote
0
down vote
Mongoose can do it for you automatically. according to timestamp option in Mongoose just do something like this :
var nSchema=new Schema({
name:String,
...
timestamps: { createdAt: 'created_at' },
...
});
also you can track for update with updateAt
add a comment |
up vote
0
down vote
up vote
0
down vote
Mongoose can do it for you automatically. according to timestamp option in Mongoose just do something like this :
var nSchema=new Schema({
name:String,
...
timestamps: { createdAt: 'created_at' },
...
});
also you can track for update with updateAt
Mongoose can do it for you automatically. according to timestamp option in Mongoose just do something like this :
var nSchema=new Schema({
name:String,
...
timestamps: { createdAt: 'created_at' },
...
});
also you can track for update with updateAt
answered Nov 11 at 7:22
MBehtemam
2,69774381
2,69774381
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25217168%2fhow-to-query-mongodb-with-mongoose-adding-created-date-in-results%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown