Rishabh Jain
Rishabh Jain

Reputation: 536

inserting in a nested array in mongoose

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);


                                            });

Upvotes: 0

Views: 390

Answers (1)

Molda
Molda

Reputation: 5704

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

Upvotes: 1

Related Questions