react-native-local-mongodb icon indicating copy to clipboard operation
react-native-local-mongodb copied to clipboard

Update array inside a object

Open ithustle opened this issue 4 years ago • 4 comments

System:
    OS: macOS 10.15.2
    CPU: (4) x64 Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
    Memory: 48.47 MB / 8.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.4.0 - /usr/local/bin/node
    Yarn: 1.22.4 - ~/.yarn/bin/yarn
    npm: 6.9.0 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
    Android SDK:
      API Levels: 23, 24, 25, 26, 27, 28
      Build Tools: 23.0.1, 23.0.3, 25.0.0, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.3, 28.0.2, 28.0.3
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6308749
    Xcode: 11.3/11C29 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.13.1 => 16.13.1 
    react-native: 0.61.5 => 0.61.5 
  npmGlobalPackages:
    react-native-clean-project: 3.2.4
    react-native-cli: 2.0.1
    react-native-git-upgrade: 0.2.7

STEPS TO REPRODUCE const upt = await tpPlayLists.updateAsync({ nome_playlist, "musicas.musicaId" : id }, { $set: { "musicas.localPath":"yes" } }, {});

Hi, @antoniopresto

This is the structure of my data stored on my BD:

{
    "_id" : ObjectId("5e923b1e6dc5d9a46968f1b5"),
    "nome_playlist" : "Show",
    "data_criacao" : ISODate("2020-03-23T17:29:19.621Z"),
    "musicas" : [ 
        {
            "assetUrl96" : "https://d37lpiggx4n4f5.cloudfront.net/gm/musicas/singles/--1-boda/--1-boda_96.mp3",
            "assetUrl192" : "https://d37lpiggx4n4f5.cloudfront.net/gm/musicas/singles/--1-boda/--1-boda_192.mp3",
            "title" : "+ 1 Boda",
            "album" : "+ 1 Boda",
            "url" : "https://d37lpiggx4n4f5.cloudfront.net/gm/musicas/singles/--1-boda/--1-boda_96.mp3",
            "id" : "1393593596728",
            "assetUrl" : "https://d37lpiggx4n4f5.cloudfront.net/gm/musicas/singles/--1-boda/02.---1-boda--feat.-kelson-most-wanted---dj-nibblez-.mp3",
            "album_title" : "+ 1 Boda",
            "artwork" : "https://d37lpiggx4n4f5.cloudfront.net/gm/musicas/singles/--1-boda/capa.jpg",
            "id_single" : "5c2d19c1b7cb0f2528e92878",
            "artist" : "GM",
            "duration" : 172.120816326531
        }, 
        {
            "duration" : 224.548571428571,
            "title" : "Perfeito",
            "bitrate" : 320000,
            "size" : 8987210,
            "play" : 0,
            "id" : 812039731068.0,
            "assetUrl" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/perfeito/nga---perfeito.mp3",
            "assetUrl192" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/perfeito/perfeito_192.mp3",
            "assetUrl96" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/perfeito/perfeito_96.mp3",
            "artwork" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/perfeito/capa-perfeito-nga.jpg",
            "cover" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/perfeito/capa-perfeito-nga.jpg",
            "album_title" : "Perfeito",
            "preco" : 0,
            "artist" : "NGA",
            "id_single" : "5bae78cd4e6d8e12cd9486f9",
            "stream" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/perfeito/perfeito_96.mp3",
            "localPath" : "Célio"
        }, 
        {
            "duration" : 238.680816326531,
            "title" : "Tatuagens, Cicatrizes & Diamantes",
            "bitrate" : 320000,
            "size" : 9552500,
            "play" : 0,
            "id" : 1043344503948.0,
            "assetUrl" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/tatuagens--cicatrizes---diamantes/nga-tatuagem-cicatrizes-diamantes.mp3",
            "assetUrl192" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/tatuagens--cicatrizes---diamantes/tatuagens--cicatrizes---diamantes_192.mp3",
            "assetUrl96" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/tatuagens--cicatrizes---diamantes/tatuagens--cicatrizes---diamantes_96.mp3",
            "artist" : "NGA",
            "album_title" : "Tatuagens, Cicatrizes & Diamantes",
            "artwork" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/tatuagens--cicatrizes---diamantes/capa-nga.jpg",
            "tipo" : "Single",
            "genero" : "Hip-Hop",
            "cover" : "https://d37lpiggx4n4f5.cloudfront.net/nga/musicas/singles/tatuagens--cicatrizes---diamantes/capa-nga.jpg",
            "preco" : 0,
            "id_single" : "5bae78584e6d8e12cd9486f8"
        }
    ]
}

I need to set a new field on array musicas using the code above but when I tried that code all the data inside array musicas are replaced. Am I missing some step?

ithustle avatar Apr 13 '20 14:04 ithustle

@antoniopresto

ithustle avatar Apr 21 '20 22:04 ithustle

You need to use $elemMatch to update a neasted array. https://docs.mongodb.com/manual/reference/operator/query/elemMatch/

BenoitClaveau avatar Jul 15 '20 10:07 BenoitClaveau

you can use $ to represent the array index you matched: musicas.$.field

antoniopresto avatar Jul 15 '20 11:07 antoniopresto

None of these solution works

ithustle avatar Mar 22 '21 17:03 ithustle