如何在猫鼬中更新所选文档中数组的所有嵌套文档?

时间:2019-06-03 13:03:20

标签: javascript node.js mongoose

在一种情况下,我需要更新嵌套在Mongoose模型中的数组中的所有文档的字段。

我将上载图像的新嵌套文档添加到“用户”集合,并且需要将所有先前文档的“ is_available”设置为false,而将新插入文档的“ is_available”字段设置为true。

我搜索并发现的所有内容都是关于使用特定ID或过滤器更新嵌套文档。但是我没有找到一种方法来更新模型中数组的所有嵌套文档的特定字段。

我的模特:

@RepositoryRestResource(itemResourceRel = "process", collectionResourceRel = "processes", path = "process")
public interface ProcessDataRestRepository extends JpaRepository<Process, Long> {

    @Query("Select p.code, p.name, sp.code, sp.name " +
            "from Process p " +
            "inner join Zone z on p.id = z.process " +
            "inner join Subprocess sp on sp.id = z.subprocess")
    List<ProcessDto> findProcessesAndSubprocesses();

}

实际结果:

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var UsersSchema = new Schema({
  name: { type: String, default: "" },
  lastname: { type: String, default: "" },
  images: [{
    dir: { type: String },
    is_available: { type: Boolean }
  }]
});

var Users = mongoose.model("Users", UsersSchema, "users");
module.exports = Users;

预期结果:


{ 
    "_id" : ObjectId("5ce950aa4sw435ty503ab83b"), 
    "name" : "Sina", 
    "image" : [
        {
            "_id" : ObjectId("5cf4dd3da6b4gb6bcf9c6dc1"), 
            "dir" : "profiledirectory/804eafeaf8acc40a3d6.jpg", 
            "is_available" : true
        }, 
        {
            "_id" : ObjectId("5cf4fffs2419316bddffgg82"), 
            "dir" : "profiledirectory/d85b01ac40fa6b1a7c1.jpg", 
            "is_available" : true
        }, 
        {
            "_id" : ObjectId("5cf4ff334fhd316bef2fdt40"), 
            "dir" : "profiledirectory/512d9d19da7a4322b37.jpg", 
            "is_available" : true
        }
    ]
}

{ 
    "_id" : ObjectId("5ce950aa4sw435ty503ab83b"), 
    "name" : "Sina", 
    "image" : [
        {
            "_id" : ObjectId("5cf4dd3da6b4gb6bcf9c6dc1"), 
            "dir" : "profiledirectory/804eafeaf8acc40a3d6.jpg", 
            "is_available" : false
        }, 
        {
            "_id" : ObjectId("5cf4fffs2419316bddffgg82"), 
            "dir" : "profiledirectory/d85b01ac40fa6b1a7c1.jpg", 
            "is_available" : false
        }, 
        {
            "_id" : ObjectId("5cf4ff334fhd316bef2fdt40"), 
            "dir" : "profiledirectory/512d9d19da7a4322b37.jpg", 
            "is_available" : true
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

上传文件后,您必须找到用户

  // after upload image 

  User.findOne({_id:req.user._id}).lean().exec((err,user)=>{
        if(!err && user){
              // setting all previous images to false 
              user.image.forEach(image=>{
                    image.is_available=false;
              });
              // now pushing new uploded item to image array
        let newImage={"dir" : "path of uploaded item here"  , "is_available" : true};

              user.image.push(newImage);
              user.save((err,updated)=>{
                   if(!err && updated){ 
                     return res.status(200).json({status:"success",message:"User updated"});
                   }
              });
        }
  });

答案 1 :(得分:0)

根据您提到的内容,您将动态地向图像字段添加对象,并且每次添加新对象时,现有对象都应使用 is_available:false

覆盖
    eventObject.find(query).lean().exec(function (err, response) {
        if (err) {
            logger.error(err);
            return reject(err.message)
        }
        // Assuming there will be only one record based on the query you make
        if(response.length>0){
            response[0].images = response.images.map((image)=>{
                image.is_available = false
                return image
            })

            // Now append your new image to the modified response
            response[0].images.push(newimage)// this should be the newImage object
            // Now use findOneAndUpdate to update the existing doc by querying with id or according to your usecase

            eventObject.findOneAndUpdate(query, response[0], { upsert: true }, (err, data) => {
            if (err) {
                return reject(err)
            }
            logger.info(data);
            return resolve(newData);
        })
        }
    })

所以基本方法是

  1. 从数据库获取文档
  2. 更新文档的内容
  3. 添加新数据
  4. 使用更新的数据更新记录。