Mongo在双嵌套数组中更新

时间:2016-08-29 06:39:45

标签: node.js mongodb multidimensional-array

我有一个看起来像这样的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数组添加到当前的exercise数组中。我在使用双嵌套数组的点符号时遇到问题

我试过这个

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,它将推送到第二个练习数组,这显然是我想要的。但我正在建立一个网站,所以我显然难以编码。我需要使用$但它似乎并没有超过第一个练习对象。

任何帮助我都会非常感激!

2 个答案:

答案 0 :(得分:1)

现在(MongoDB> = 3.6)使用R50 summary outputarrayFilters进行此操作。

以下示例使用mongoose,并将项目添加到双嵌套数组内的数组中。解释这一点的好文章是$[identifier]

  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);

答案 1 :(得分:0)

db.users.update({
  'email': 'jeffreyyourman@gmail.com', 
  "workout.workoutId": "Bkb6HIWs",
  "workout.exercises.exerciseId":"ByU8II-s"
},

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

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

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

但我认为您可能需要重新设计架构以获得更好的性能。也许为锻炼和锻炼做一个模式,然后使用引用来连接它们。 http://mongoosejs.com/docs/populate.html