繁体   English   中英

循环遍历对象数组并更新每个对象

[英]Looping Over And Array of Objects and Updating Each One

我正在尝试根据我使用两个函数检查的特定条件更新数组中的对象。 我无法使用mongoose中提供的match功能来实现这一点。

正如您在下面看到的,我正在尝试遍历帖子数组,发送每个帖子并检查它是否满足条件,然后使用findOneAndUpdate对其进行更新。 函数isLessThanOneisAchieved返回预期值。

(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' }})
    }
  })
  
})()

这是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)

问题是我无法更新帖子,我已经尝试过使用 udpate ,直接用udpate更新它post.category然后save 任何帮助将不胜感激。

尝试这个:

(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' }})
    }
  })
  
})()

如果我记得,过滤器参数必须是一个对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM