0

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

1 Answer 1

1

$each is expected with an array value.

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

Sample Mongo Playground

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I see, but how would I go about inserting the newPlaylistSongs variable into the collection? since it's already an array. Edit: never mind, I misread your solution. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.