简体   繁体   English

通过更改数组中的对象来更新MongoDB中的多个文档

[英]Update multiple documents in MongoDB by altering object in array

I have a simple application with registration/login and it is basically a coursera/udemy type, where the app lists specific courses and users can like them or enroll in them. 我有一个简单的注册/登录应用程序,它基本上是一个Coursera / udemy类型,该应用程序列出了特定的课程,用户可以喜欢它们或注册他们。 I have been trying to make a mongodb function that updates a user in the database and since users can like the courses it has to update all courses too (courses have a field "usersLiked", which is an array and keep all user documents which have liked it). 我一直在尝试使用mongodb函数来更新数据库中的用户,并且由于用户可以喜欢课程,因此它也必须更新所有课程(课程具有“ usersLiked”字段,该字段是一个数组,并保留所有具有喜欢它)。

The course structure is the following: 课程结构如下:

{
    "_id" : ObjectId("5977662564aac9f6c8d48884"),
    "title" : "Title",
    "lecturer" : "Lecturer",
    "length" : 30,
    "coverPhoto" : "Photo",
    "usersLiked": [ 
        {
            "_id" : ObjectId("597763e346a7a463cbb8f529"),
            "fullname" : "Name",
            "username" : "Username",
            "password" : "Hash",
            "city" : "City",
            "street" : "Street",
            "website" : "Website"
        }
    ],
    "lectures" : [ 
        {
            "title" : "Introduction",
            "number" : 1,
            "url" : "someURL"
        }
    ]
}

And the user structure: 和用户结构:

{
    "_id" : ObjectId("597763e346a7a463cbb8f529"),
    "fullname" : "Name",
    "username" : "Username",
    "password" : "Hash",
    "enrolledCourses" : [ 
        ...
            ]
        }
    ],
    "city" : "City",
    "street" : "Street",
    "website" : "Website"
}

So now I am calling this function when I want to change. 所以现在我要更改时调用此函数。 It changes the userCollection but in the courseCollection it does nothing, while it should get all courses and if some of them have an object with username(the user's username) in the "usersLiked" array it should modify the user there too. 它更改了userCollection,但是在courseCollection中它什么也不做,尽管它应该获取所有课程,并且如果其中一些课程在“ usersLiked”数组中具有用户名(用户名)的对象,它也应该在那里修改用户。

const updateUser = (username, details) => {
        usersCollection
            .update({
                username: username,
            }, {
                $set: {
                    fullname: details.fullname,
                    city: details.city,
                    street: details.street,
                    website: details.website,
                },
            });
        coursesCollection
            .updateMany(
                {
                    usersLiked: {
                        $elemMatch: {
                            username: username,
                        },
                    },
                },
                {
                    $set: {
                        'usersLiked.username': details.username,
                        'usersLiked.city': details.city,
                        'usersLiked.street': details.street,
                        'usersLiked.website': details.website,
                    },
                }
            );
    };

Your match on the course update looks valid but you are trying to set values into an array and according to the Mongo docs you need to provide an array indexer or a positional operator. 您在课程更新中的匹配看起来有效,但是您正在尝试将值设置为数组,并且根据Mongo文档,您需要提供数组索引器或位置运算符。

The following will allow the set command to operate on the first element within the usersLiked array which matches the given username . 下面将允许set命令对usersLiked数组中与给定username匹配的第一个元素进行操作。

coursesCollection.updateMany(
    {
        usersLiked: {
            $elemMatch: {
                username: username,
            },
        },
    },
    {
        $set: {
            'usersLiked.$.username': details.username,
            'usersLiked.$.city': details.city,
            'usersLiked.$.street': details.street,
            'usersLiked.$.website': details.website
        },
    }
)

You could also choose which element in the usersLiked array to update eg usersLiked.1.username but I suspect each course only has one element in usersLiked for a given username in which case using the $ operator (which means: the first matching array element) should suffice. 您也可以在usersLiked数组中选择要更新的元素,例如usersLiked.1.username但我怀疑对于给定的username ,每门课程在usersLiked只有一个元素,在这种情况下,使用$运算符(这意味着:第一个匹配的数组元素)应该足够了。

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

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