简体   繁体   English

更新 MongoDB 集合文档中的嵌套键值

[英]Updating a nested key value in a MongoDB collection document

I have this collection in my MongoDB (Atlas) database, that represents exercise logs for users:我的 MongoDB (Atlas) 数据库中有这个集合,它代表用户的锻炼日志:

[
  {
    _id: ObjectId("...") <--- Exercise ID
    exerciseName: "Bench Press",
    sets: [
      {
        _id: ObjectId("...") <--- Set ID
        weight: 100
        reps: 10
        createdAt: 1614461387587
        notes: ""
      },
      {
        // More set data objects
      }
    ]
  },
  {
    // More exercise data objects
  }
]

I am creating a functionality in my app that allows users to edit a set, incase they input it incorrectly.我正在我的应用程序中创建一个功能,允许用户编辑一组,以防他们输入不正确。 Currently, I do this by fetching the user's exercise that the set belongs to with:目前,我通过获取该集合所属的用户练习来做到这一点:

const user = checkAuth(context);
let exerciseLog = await Exercise.findOne({
  _id: exerciseId,
  user: user.id,
  "sets._id": setId,
});

Which gets the correct exercise log.哪个得到正确的锻炼日志。 From there, I thought I could use the.find() function to get the set that needs editing and replace the values with new ones like so:从那里,我想我可以使用 the.find() function 来获取需要编辑的集合并用新的值替换值,如下所示:

const set = exerciseLog.sets.find((set) => set.id === setId);
if (set) {
  const newSet = { ...set, weight, reps, notes };
  const setIndex = exerciseLog.sets.findIndex(
    (set) => set.id === setId
  );
  exerciseLog.sets.splice(setIndex, 1, newSet);
  await exerciseLog.save();
}

However, this approach once executed, does replace the values for weight, reps and notes but deletes the createdAt key entirely and changes the set id to a new one.然而,这种方法一旦执行,确实会替换重量、次数和音符的值,但会完全删除 createdAt 键并将 set id 更改为新的。 I thought that by using the spread operator, values for the object would be retained except the ones that follow the operator?我认为通过使用扩展运算符,除了运算符后面的值之外,object 的值将被保留? How can I implement a solution in which I keep the old id and createdAt, while updating the weight, reps and notes values in the database?如何实现一个解决方案,在该解决方案中保留旧的 id 和 createdAt,同时更新数据库中的 weight、reps 和 notes 值?

You can do both find and update in single query Try this:您可以在单个查询中进行findupdate试试这个:

db.Workout.updateOne(
    {
        "sets._id": ObjectId("6039709fe0c7d52970d3fa2f")
    },
    {
        $set: {
            "sets.$.weight": 101,
            "sets.$.reps": 11,
            "sets.$.notes": "Updated"
        }
    }
);

Mongoose: Mongoose:

Exercise.updateOne(
    {
        "sets._id": ObjectId("6039709fe0c7d52970d3fa2f")
    },
    {
        "sets.$.weight": 101,
        "sets.$.reps": 11,
        "sets.$.notes": "Updated"
    }
).exec();

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

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