简体   繁体   English

ZCCADCDEDB567ABAE643E15DCF0974E503Z - 将对象推入嵌套数组

[英]Mongoose - Push objects into nested array

I am trying to push every object inside of an array into an array using Mongoose.我正在尝试使用 Mongoose 将数组内的每个 object 推入一个数组中。

This is an example of what the structure of my playlist collection looks like:这是我的播放列表集合结构的示例:

playlist: {
    userName: 'user-name',
    userId: 'user-id',
    playlistArray: [
        {
            playlistName: 'playlist1',
            playlistSongs: [
                { title: '', duration: 0, uri: '' },
                { title: '', duration: 0, uri: '' },
                { title: '', duration: 0, uri: '' },
            ],
        },
        {
            playlistName: 'playlist2',
            playlistSongs: [
                { title: '', duration: 0, uri: '' },
                { title: '', duration: 0, uri: '' },
                { title: '', duration: 0, uri: '' },
            ],
        },
    ],
}

I want to push the following array of objects into the playlistSongs array that also has playlistName: 'playlist2' within the same object我想将以下对象数组推送到同样具有playlistName: 'playlist2'playlistSongs数组中 object

[
    { title: '1', duration: 123, uri: 'a' },
    { title: '2', duration: 456, uri: 'b' },
    { title: '3', duration: 789, uri: 'c' },
]

So the result would look like this:所以结果看起来像这样:

playlist: {
    userName: 'user-name',
    userId: 'user-id',
    playlistArray: [
        {
            playlistName: 'playlist1',
            playlistSongs: [
                { title: '', duration: 0, uri: '' },
                { title: '', duration: 0, uri: '' },
                { title: '', duration: 0, uri: '' },
            ],
        },
        {
            playlistName: 'playlist2',
            playlistSongs: [
                { title: '', duration: 0, uri: '' },
                { title: '', duration: 0, uri: '' },
                { title: '', duration: 0, uri: '' },
                { title: '1', duration: 123, uri: 'a' },
                { title: '2', duration: 456, uri: 'b' },
                { title: '3', duration: 789, uri: 'c' },
            ],
        },
    ],
}

Here is my code trying to update the model:这是我尝试更新 model 的代码:

const newPlaylistSongs = [
    { title: '1', duration: 123, uri: 'a' },
    { title: '2', duration: 456, uri: 'b' },
    { title: '3', duration: 789, uri: 'c' },
];

await playlist
.updateOne(
    {},
    {
        $addToSet: {
            'playlistArray.$[pl].playlistSongs': {
                $each: {
                    newPlaylistSongs
                }
            },
        },
    },
    {
        arrayFilters: [
            {
                'pl.playlistName':'playlist2',
            },
        ],
    }
)
.catch(console.error);

But I have gotten this error:但我收到了这个错误:

MongoServerError: The argument to $each in $addToSet must be an array but it was of type object MongoServerError:$addToSet 中 $each 的参数必须是一个数组,但它的类型是 object

$each is expected with an array value. $each需要一个array值。

$each: []
await playlist
.updateOne(
    {},
    {
        $addToSet: {
            'playlistArray.$[pl].playlistSongs': {
                $each: newPlaylistSongs
            },
        },
    },
    {
        arrayFilters: [
            {
                'pl.playlistName':'playlist2',
            },
        ],
    }
)

Sample Mongo Playground示例 Mongo Playground

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

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