Home > Software engineering >  Looping Over And Array of Objects and Updating Each One
Looping Over And Array of Objects and Updating Each One

Time:07-16

I'm trying to update objects inside an array based on specific conditions that I check using two functions. I couldn't achieve this with the match feature available in mongoose.

As you can see below, I'm trying to loop over the array of posts, send each post and check if it fulfills a condition, then update it using findOneAndUpdate. The functions isLessThanOne and isAchieved return the expected value.

(async () => {

  let posts = await Post.find()

  posts.map(async (post) => {
    if (isLessThanOneDay(post.deadline)) {
      console.log(post.id, post._id)
      await Post.findOneAndUpdate(post.id, { $set: { category: 'deadline' }})
    } else if (isAchieved(post.milestones)) {
      await Post.findOneAndUpdate(post.id, { $set: { category: 'achieved' }})
    } else {
      await Post.findOneAndUpdate(post.id, { $set: { category: 'newGoals' }})
    }
  })
  
})()

This is the schema

const { type } = require("express/lib/response")
const mongoose = require("mongoose")

const PostSchema = new mongoose.Schema({
    // we have to link or refer each post to a user
    user: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "user",
    },
    title: {
        type: String,
        required: true
    },
    desc: {
        type: String
    },
    milestones: [],
    likes: [
        {
            user: {
                type: mongoose.Schema.Types.ObjectId,
                ref: "user"
            },
            fName: {
                type: String,
                required: true
            },
            date: {
                type: String,
                required: true
            }

        }
    ],
    comments: [
        {
            user: {
                type: mongoose.Schema.Types.ObjectId,
                ref: "user"
            },
            text: {
                type: String,
                required: true
            },
            fName: {
                type: String,
            },
            date: {
                type: String,
            }
        }
    ],
    rewards: [
        {
            user: {
                type: mongoose.Schema.Types.ObjectId,
                ref: "user"
            },
            fName: {
                type: String,
                required: true
            },
            date: {
                type: String,
                required: true
            },
            reward: {
                type: String,
                required: true
            },
            price: {
                type: Number,
                required: true
            }
        }
    ],
    date: {
        type: String,
        required: true
    },
    shares: [
        {
            user: {
                type: mongoose.Schema.Types.ObjectId,
                ref: "user"
            },
            fName: {
                type: String,
                required: true
            },
            date: {
                type: String,
                required: true
            }

        }
    ],
    deadline: {
        type: String,
        required: true
    },
    category: {
        type: String,
    }


}) 

module.exports = Post = mongoose.model('post', PostSchema)

The problem is that I couldn't update the posts and I've already tried using udpate, directly updating it with post.category then save. Any help would appreciated.

CodePudding user response:

Try this:

(async () => {

  let posts = await Post.find()

  posts.map(async (post) => {
    if (isLessThanOneDay(post.deadline)) {
      console.log(post.id, post._id)
      await Post.findOneAndUpdate({ _id: post.id }, { $set: { category: 'deadline' }})
    } else if (isAchieved(post.milestones)) {
      await Post.findOneAndUpdate({ _id: post.id }, { $set: { category: 'achieved' }})
    } else {
      await Post.findOneAndUpdate({ _id: post.id }, { $set: { category: 'newGoals' }})
    }
  })
  
})()

If I recall, the filter parameter must be an object.

  • Related