0

i have a mongoose model something like

module.exports = mongoose.model('ContactBirthday', {
    email: {
        type: String,
        unique: true
    },
    birthday: {
        "1": {
            "1": [
                {
                    "firstName": String,
                }
            ],
            "2": [
                {
                    "firstName": String,
                }
            ]
         }
     }
   }

i want to push a value in birthday.1.2 . I have the value of 1 and 2 in variables bMonth and bDate , and using the following code to push , but somehow only email is inserted

var bMonth = req.body.contact.birthday.month;
var bDate = req.body.contact.birthday.date;

                                            ContactBirthday.findOneAndUpdate({
                                                email: result.message.email
                                            }, {
                                                $push: {
                                                    birthday: {
                                                        bMonth: {
                                                            bDate: {
                                                                "firstName": req.body.contact.birthday.firstName,
                                                                "_id": data[0].contacts[data[0].contacts.length - 1]._id
                                                            }
                                                        }
                                                    }
                                                }
                                            }, {
                                                upsert: true
                                            }, function (err, result) {
                                                if (err)
                                                    return res.send(500, {
                                                        error: err
                                                    });
                                                else
                                                    res.sendStatus(200);


                                            });
6
  • bDate: { "firstName": "value" } is an object with key bDate! It's not actually { 12/11/2015: { ... Also don't do that kind of nasty nasting! Commented Feb 23, 2016 at 15:16
  • @AndreyPopov any ideas how to store birthday of every user in form of month and date ? :) Commented Feb 23, 2016 at 15:18
  • Mongoose has Date type in it! :) Just make it a field inside the User schema, if you have one Commented Feb 23, 2016 at 16:24
  • i can do that , in fact i am doing that , but i also need a birthday collection , where everyday i will check for today's birthday and send mails , so i thought i would save a birthday object with nesting of month and day , then i will be able to extract that particular day directly @AndreyPopov Any help on this part please :) Commented Feb 23, 2016 at 17:11
  • Why don't you add birthday to your contact model and query it every day like Contact.find({'birthday: {$gte: new Date(2016, 0, 1), $lt : new Date( 2016, 0, 2)}, function.....); to get all contacts with birthday in 1.1.2016 Commented Feb 23, 2016 at 17:40

1 Answer 1

1

You can add birthday to your contact model and query it every day like

 Contact.find({
      'birthday': {
           $gte: new Date(2016, 0, 1), 
           $lt : new Date( 2016, 0, 2)
      }
 }, function(err, results){
 ...
 }); 

to get all contacts with birthday in 1.1.2016

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.