Created
June 23, 2019 23:10
-
-
Save syedsouban/4e273ac6cb04a03fde49e77b9e66ccd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Board.find({}) | |
.populate({ | |
path: 'pins', | |
model: 'Pin' | |
}) | |
.exec(function(err,boards){ | |
var dates=[]; | |
boards.forEach(function(board){ | |
dates=[]; | |
board.pins.forEach(function(pin){ | |
dates.push(pin.createdAt); | |
}); | |
Board.findOneAndUpdate({_id: board._id}, {createdAt: Math.min(...dates)},function(err,pin){ | |
if(!err&&pin) | |
console.log(pin); | |
}); | |
}); | |
//Board schema and pin schema given below for reference | |
//const boardSchema = new mongoose.Schema({ | |
// name: { | |
// type: String, | |
// required: true | |
// }, | |
// description: String, | |
// secret: Boolean, | |
// topic: String, | |
// createdBy: { type: ObjectId, ref: 'User'}, | |
// pins: [{type: ObjectId, ref: 'Pin'}], | |
// createdAt: Date, | |
// updatedAt: Date | |
// }); | |
// const pinSchema = new mongoose.Schema({ | |
// title: { | |
// type: String, | |
// required: true | |
// }, | |
// url: { | |
// type: String, | |
// required: true | |
// }, | |
// description: { | |
// type: String, | |
// }, | |
// website: String, | |
// tags : [String], | |
// createdBy: { type: ObjectId, ref: 'User'}, | |
// boardId:{type: ObjectId, ref: 'Board'}, | |
// createdAt: Date, | |
// updatedAt: Date | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment