Mongo在一个双嵌套数组中更新



我有一个mongo集合,看起来像这个

db.users.find().pretty()
{
    "_id" : ObjectId("57c3d5b3d364e624b4470dfb"),
    "fullname" : "tim",
    "username" : "tim",
    "email" : "tim@gmail.com",
    "password" : "$2a$10$.Z9CnK4oKrC/CujDKxT6YutohQkHbAANUoAHTXQp.73KfYWrm5dY2",
    "workout" : [
        {
            "workoutId" : "Bkb6HIWs",
            "workoutname" : "chest day",
            "BodyTarget" : "Chest",
            "date" : "Monday, August 29th, 2016, 2:27:04 AM",
            "exercises" : [
                {
                    "exerciseId" : "Bym88LZi",
                    "exercise" : "bench press",
                    "date" : "Monday, August 29th, 2016, 2:29:30 AM"
                },
                {
                    "exerciseId" : "ByU8II-s",
                    "exercise" : "flys",
                    "date" : "Monday, August 29th, 2016, 2:29:34 AM"
                }
            ]
        },
        {
            "workoutId" : "Bk_TrI-o",
            "workoutname" : "Back day",
            "BodyTarget" : "Back",
            "date" : "Monday, August 29th, 2016, 2:27:12 AM"
        }
    ]
}

所以我最终希望它看起来像这个

db.users.find().pretty()
{
    "_id" : ObjectId("57c3d5b3d364e624b4470dfb"),`enter code here`
    "fullname" : "tim",
    "username" : "tim",
    "email" : "tim@gmail.com",
    "password" : "$2a$10$.Z9CnK4oKrC/CujDKxT6YutohQkHbAANUoAHTXQp.73KfYWrm5dY2",
    "workout" : [
        {
            "workoutId" : "Bkb6HIWs",
            "workoutname" : "chest day",
            "BodyTarget" : "Chest",
            "date" : "Monday, August 29th, 2016, 2:27:04 AM",
            "exercises" : [
                {
                    "exerciseId" : "Bym88LZi",
                    "exercise" : "bench press",
                    "date" : "Monday, August 29th, 2016, 2:29:30 AM",
                    "stats" : [
                          {
                            "reps: '5',
                            "weight":'105'
                          }
                },
                {
                    "exerciseId" : "ByU8II-s",
                    "exercise" : "flys",
                    "date" : "Monday, August 29th, 2016, 2:29:34 AM"
                }
            ]
        },
        {
            "workoutId" : "Bk_TrI-o",
            "workoutname" : "Back day",
            "BodyTarget" : "Back",
            "date" : "Monday, August 29th, 2016, 2:27:12 AM"
        }
    ]
}

我想将stats数组添加到当前的练习数组中。我在双嵌套数组的点表示法方面遇到了问题

我试过这个

db.users.update({
  'email': 'jeffreyyourman@gmail.com', "workout.workoutId": "Bkb6HIWs" ,"workout.exercises.exerciseId":"ByU8II-s"
},
{
  $push: {
          "workout.0.exercises.$.stats": {"sets":"sets", "reps":"reps"}}})

它实际上有效,但总是会推到第一个嵌套的练习对象。

现在如果我这样做。。。

db.users.update({
  'email': 'jeffreyyourman@gmail.com', "workout.workoutId": "Bkb6HIWs" ,"workout.exercises.exerciseId":"ByU8II-s"
},
{
  $push: {
          "workout.0.exercises.1.stats": {"sets":"sets", "reps":"reps"}}})

把$换成1,它会推到第二个练习数组,这显然是我想要的。但我正在建一个网站,所以我显然不能硬编码。我需要使用$,但它似乎无法通过第一个练习对象。

如果有任何帮助,我将不胜感激!

现在(MongoDB>=3.6(有一种方法可以通过arrayFilters和$[identifier]来实现这一点。

下面的示例使用mongoose,并将一个项添加到双嵌套数组内的数组中。这里有一篇很好的文章来解释这一点。

  const blogPost = await BlogPost.create({
    title    : 'A Node.js Perspective on MongoDB 3.6: Array Filters',
    comments : [
      { author : 'Foo', text : 'This is awesome!', replies : { name : 'George', seenBy : ['Pacey'] } },
      { author : 'Bar', text : 'Where are the upgrade docs?', replies : { name : 'John', seenBy : ['Jenny'] } }
    ]
  });
  const updatedPost = await BlogPost.findOneAndUpdate({ _id : blogPost._id }, {
    $addToSet : {
      'comments.$[comment].replies.$[reply].seenBy' : 'Jenny'
    }
  }, {
    arrayFilters : [{ 'comment.author' : 'Foo' }, { 'reply.name' : 'George' }],
    new          : true
  });
  console.log(updatedPost.comments[0].replies);
db.users.update({
  'email': 'jeffreyyourman@gmail.com', 
  "workout.workoutId": "Bkb6HIWs",
  "workout.exercises.exerciseId":"ByU8II-s"
},

这个查询部分返回第一个匹配对象,它是整个用户配置文件,而不是一个练习条目。因此,您可以指定电子邮件来获取相同的对象。"workoutId"one_answers"exerciseId"是多余的。

{
  $push: {
    "workout.0.exercises.$.stats": {"sets":"sets", "reps":"reps"}
  }
})

因此,这个推送命令只会像Amiram在评论中所说的那样,推送到第一次进入训练。在这种情况下,可以检索整个对象,对其进行修改,然后保存。

但我认为您可能需要重新设计您的模式以获得更好的性能。也许可以为锻炼和锻炼制定一个模式,然后使用参考将它们联系起来。http://mongoosejs.com/docs/populate.html

相关内容

  • 没有找到相关文章

最新更新