Skip to content

Instantly share code, notes, and snippets.

@syedsouban
Created June 23, 2019 23:10
Show Gist options
  • Save syedsouban/4e273ac6cb04a03fde49e77b9e66ccd6 to your computer and use it in GitHub Desktop.
Save syedsouban/4e273ac6cb04a03fde49e77b9e66ccd6 to your computer and use it in GitHub Desktop.
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