mongodb 修改mongoose结果将修改所有文档而不是一个文档

wooyq4lh  于 2022-11-22  发布在  Go
关注(0)|答案(1)|浏览(167)

我有这个功能:

const getXNumberOfDocuments = async function getXNumberOfDocuments(
  page,
  results_per_page
) {
  /*************************************************** */
  results_per_page = parseInt(results_per_page);
  let x_number_of_documents = await Document.find()
    .populate([
      {
        path: "user",
        populate: {
          path: "profile",
          select: ["profileImageURL"],
        },
      },
    ])
    .limit(results_per_page)
    .skip(results_per_page * page)
    .lean();
  /*************************************************** */
  // I will stop this loop at i=0 by throwing an error
  // As you se below
  for (let i = 0; i < x_number_of_documents.length; i++) {
    console.log("🚀 ~ file: documentsServices.js ~ line 295 ~ i", i);
    // #BUG:
    // For some reason this changes
    // profileImageURL for i=0 and i=1 as well
    x_number_of_documents[i].user.profile.profileImageURL =
      "THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!";
    // await s3_config.getImageReadSignedUrl(
    //   x_number_of_documents[i].user.profile.profileImageURL
    // );

    /*************************************************** */
    // I check all documents here
    // And I find that profileImageURL was modified in all of them
    // Even though I stopped the loop at i=0
    for (let j = 0; j < x_number_of_documents.length; j++) {
      console.log(
        "🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j,",
        j
      );
      console.log(
        "🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL",
        x_number_of_documents[j].user.profile.profileImageURL
      );
      console.log(
        "🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 "
      );
    }
    /*************************************************** */
    throw new Error("STOPPING LOOP AT i=0");
    /*************************************************** */
  }
  return x_number_of_documents;
};

它通常会返回如下内容:

const response = [
    {
      user: {
        profile: {
          profileImageURL: "https://xxxxxxx.com/xxx.jpeg",
        },
      },
    },
    {
      user: {
        profile: {
          profileImageURL: "https://xxxxxxx.com/yyy.jpeg",
        },
      },
    },
    {
      user: {
        profile: {
          profileImageURL: "https://xxxxxxx.com/zzz.jpeg",
        },
      },
    },
  ];

我尝试修改每个对象中的profileImageURL

for (let i = 0; i < x_number_of_documents.length; i++) {
  console.log("🚀 ~ file: documentsServices.js ~ line 295 ~ i", i);
  x_number_of_documents[i].user.profile.profileImageURL =
    "THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!";
}

问题是我发现当i==0时,它不是修改profileImageURLONLY第一个对象,而是修改所有对象
因此,当我在i==0之后中断循环,并记录所有对象时,我看到profileImageURL在所有对象中都是相同的:

🚀 ~ file: documentsServices.js ~ line 291 ~ x_number_of_documents.length 5
🚀 ~ file: documentsServices.js ~ line 295 ~ i 0

🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 0
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 1
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 2
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 3
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 j, 4
🚀 ~ file: documentsServices.js ~ line 299 ~ x_number_of_documents[j].user.profile.profileImageURL THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!
3asba
🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀 
🚀 ~ file: documents.js ~ line 204 ~ err Error: STOPPING LOOP AT i=0

知道发生什么事了吗?

p8h8hvxi

p8h8hvxi1#

如果只想更新数组的第一个对象,可以通过添加条件来中断循环

for (let i = 0; i < x_number_of_documents.length; i++) {
  if (i > 0) break;
  console.log("🚀 ~ file: documentsServices.js ~ line 295 ~ i", i);
  x_number_of_documents[i].user.profile.profileImageURL =
    "THIS SHOULD ONLY BE MODIFIED FOR i=0 BUT IT IS MODIFIED FOR ALL OF THEM!";
}

如果要求只更新数组的第一个对象,则不需要遍历数组,可以执行以下操作

if(x_number_of_documents.length > 0){
    x_number_of_documents[0].user.profile.profileImageURL = "New URL";
}

相关问题