简体   繁体   中英

Mongoose - Push objects into nested array

I am trying to push every object inside of an array into an array using Mongoose.

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

[
    { 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:

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

$each is expected with an array value.

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

Sample Mongo Playground

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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