【问题标题】:Meteor.users : $addToset in Profile [nested object]Meteor.users : 配置文件中的 $addToset [嵌套对象]
【发布时间】:2015-03-23 00:37:47
【问题描述】:

每次用户订阅“街道”或其他“用户”时,我都想将他们的 ID 添加到当前用户的个人资料中的嵌套对象中。

我尝试了以下不同的结果:

Meteor.users.update(Meteor.userId(), {
        $addToSet: {
            'profile.subscription': { Street : streetDis
            }
        }
})

这会更新配置文件,但每次都会在数组中创建一个条目:

Profile.subscription[0] :  Street : "eziajepozjaoeja" 
Profile.subscription[1] : Street : "eezapoezkaejz"
Profile.subscription[2] : User : "akzejpazjepza"

我想要的架构如下:

Profile.subscription.Street[0] : "eziajepozjaoeja"
Profile.subscription.Street[1]:"eezapoezkaejz"
Profile.subscription.User[0]: "akzejpazjepza"

所以我试试:

Meteor.users.update(Meteor.userId(), {
            $addToSet: {
                'a.profile.last.Adress': "akzejpazjepza"}
            }
    )

返回:更新失败:访问被拒绝

Meteor.users.update(Meteor.userId(), {
            $addToSet: {
                'a.profile': { last: {Adress : "akzejpazjepza"}}
            }
    })

这也会返回:更新失败:访问被拒绝

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    如果您希望 profile.subscription.streets 和 profile.subscription.users 都是一个 id 数组,那么您应该像这样更新用户的文档:

    Meteor.users.update(Meteor.userId(), {
      $addToSet: {
        'profile.subscription.streets': streetId,
        'profile.subscription.users': userId
      }
    });
    

    【讨论】:

    • 非常感谢,最简单的解决方案效果最好!
    猜你喜欢
    • 1970-01-01
    • 2012-09-21
    • 2019-02-17
    • 1970-01-01
    • 2021-11-09
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    相关资源
    最近更新 更多