Home > Software design >  mongoose push every object inside of an array
mongoose push every object inside of an array

Time:03-13

I am trying to push every object inside of an array into a MongoDB 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

CodePudding user response:

$each is expected with an array value.

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

Sample Mongo Playground

  • Related