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?










share|improve this question




























    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?










    share|improve this question


























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 10 '14 at 14:15









      Stennie

      45.5k8108138




      45.5k8108138










      asked Aug 9 '14 at 9:36









      Jorre

      7,1472377126




      7,1472377126
























          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.






          share|improve this answer






























            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},
            ...

            });





            share|improve this answer




























              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






              share|improve this answer





















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                 

                draft saved


                draft discarded


















                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

























                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.






                share|improve this answer



























                  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.






                  share|improve this answer

























                    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.






                    share|improve this answer














                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 9 '14 at 11:45

























                    answered Aug 9 '14 at 10:57









                    ma08

                    2,2941330




                    2,2941330
























                        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},
                        ...

                        });





                        share|improve this answer

























                          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},
                          ...

                          });





                          share|improve this answer























                            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},
                            ...

                            });





                            share|improve this answer












                            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},
                            ...

                            });






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 9 '14 at 12:04









                            SUNDARRAJAN K

                            1,60221434




                            1,60221434






















                                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






                                share|improve this answer

























                                  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






                                  share|improve this answer























                                    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






                                    share|improve this answer












                                    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







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 11 at 7:22









                                    MBehtemam

                                    2,69774381




                                    2,69774381






























                                         

                                        draft saved


                                        draft discarded



















































                                         


                                        draft saved


                                        draft discarded














                                        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





















































                                        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







                                        Popular posts from this blog

                                        List item for chat from Array inside array React Native

                                        Thiostrepton

                                        Caerphilly