Mongoose - 在匹配元素后将对象推入嵌套数组
Mongoose - Push an object into a nested array after matching element
我试图在对象数组中找到匹配元素后将对象推入数组。
这是我的播放列表集合结构的示例:
playlist: {
userName: 'user-name',
userId: 'user-id',
playlistArray: [
{
playlistName: 'playlist1',
playlistSongs: [
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
],
},
{
playlistName: 'playlist2',
playlistSongs: [
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
],
},
],
}
这是我要插入到 playlistSongs
数组中的对象,该数组还包含同一对象中的 playlistName: 'playlist2'
。
{ track: 'sometrackinfo', title: 'sometracktitle', duration: 123, uri: 'sometrackuri' }
所以结果应该是这样的:
playlist: {
userName: 'user-name',
userId: 'user-id',
playlistArray: [
{
playlistName: 'playlist1',
playlistSongs: [
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
],
},
{
playlistName: 'playlist2',
playlistSongs: [
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: 'sometrackinfo', title: 'sometracktitle', duration: 123, uri: 'sometrackuri' },
],
},
],
}
使用 arrayFilters
和过滤 $[<identifier>]
运算符。
db.collection.update({},
{
"$push": {
"playlist.playlistArray.$[pl].playlistSongs": {
track: "sometrackinfo",
title: "sometracktitle",
duration: 123,
uri: "sometrackuri"
}
}
},
{
arrayFilters: [
{
"pl.playlistName": "playlist2"
}
]
})
我试图在对象数组中找到匹配元素后将对象推入数组。
这是我的播放列表集合结构的示例:
playlist: {
userName: 'user-name',
userId: 'user-id',
playlistArray: [
{
playlistName: 'playlist1',
playlistSongs: [
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
],
},
{
playlistName: 'playlist2',
playlistSongs: [
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
],
},
],
}
这是我要插入到 playlistSongs
数组中的对象,该数组还包含同一对象中的 playlistName: 'playlist2'
。
{ track: 'sometrackinfo', title: 'sometracktitle', duration: 123, uri: 'sometrackuri' }
所以结果应该是这样的:
playlist: {
userName: 'user-name',
userId: 'user-id',
playlistArray: [
{
playlistName: 'playlist1',
playlistSongs: [
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
],
},
{
playlistName: 'playlist2',
playlistSongs: [
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: '', title: '', duration: 0, uri: '' },
{ track: 'sometrackinfo', title: 'sometracktitle', duration: 123, uri: 'sometrackuri' },
],
},
],
}
使用 arrayFilters
和过滤 $[<identifier>]
运算符。
db.collection.update({},
{
"$push": {
"playlist.playlistArray.$[pl].playlistSongs": {
track: "sometrackinfo",
title: "sometracktitle",
duration: 123,
uri: "sometrackuri"
}
}
},
{
arrayFilters: [
{
"pl.playlistName": "playlist2"
}
]
})