Cannot modify the return value even if lean is used in mongoose?

  1. uses nodejs + mongoose
  2. the code is as follows
  const pkgInfo = await PkgModel.findOne({ _id }).populate({
    path: "cover",
    populate: {
      path: "tag"
    },
  }).populate({
    path: "files",
    populate: {
      path: "tag"
    },
    options: [{
      lean: true,
    }],
  }).lean(true);

  pkgInfo.files[0].fileUrl = "{a:1}";
  // 
  res.send({
    status: 1,
    msg: "success",
    data: pkgInfo,
  });

wants to add a fileUrl attribute to files [0]; the
breakpoint finds that the files [0] object has a "fileUrl" attribute, but the pkgInfo received after send does not have this attribute;

try to send the existing attribute pkgInfo.files [0] .key ="11 minutes to send normally, but if pkgInfo.files [0] .key = {}, the key remains unchanged after send, and the type of key is String;

.

3. How do I add a fileUrl attribute to each object in pkgInfo.files?

< hr >

seems to have found the reason

  }).populate({
    path: "files",
    populate: {
      path: "tag"
    },

the above code wants populate tag, but files is not defined in tag,model, but there is no value in document;
after removing populate tag, it is found that pkgInfo.files can be modified normally;
but why?

Nov.19,2021
Menu