Mongo 在双嵌套数组中更新 [英] Mongo updating inside a double nested array

查看:28
本文介绍了Mongo 在双嵌套数组中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a mongo collection that looks like this

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"
        }
    ]
}

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

So i eventually would want it to look like this

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 数组添加到当前的练习数组中.我在使用双嵌套数组的点表示法时遇到问题

I want to add the stats array to the current exercise array. I am having trouble with the dot notation with a double nested array

我试过了

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

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

Which actually works but will ALWAYS push to the first nested exercises object.

现在如果我这样做...

now if i do this...

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 替换 $ 它将推送到第二个练习数组,这显然是我想要的.但是我正在建立一个网站,所以我显然不能对其进行硬编码.我需要使用 $ 但它似乎没有让它通过第一个练习对象.

and replace the $ with a 1 it will push to the second exercises array which is obviously what i want. But i am building a website so i can't obviously hard code that in. I need to use the $ but it doesn't seem to make it past the first exercises object.

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

Any help i would greatly appreciate it !

推荐答案

现在 (MongoDB >=3.6) 有一种方法可以使用 arrayFilters$[标识符].

There's now (MongoDB >=3.6) a way to do this with the arrayFilters and $[identifier].

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

The below example is using mongoose and will add an item into an array inside a double nested array. A nice article explaining this is here.

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

这篇关于Mongo 在双嵌套数组中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆