Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update array inside a object #68

Open
ithustle opened this issue Apr 13, 2020 · 4 comments
Open

Update array inside a object #68

ithustle opened this issue Apr 13, 2020 · 4 comments

Comments

@ithustle
Copy link
Contributor

ithustle commented Apr 13, 2020

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
Copy link
Contributor Author

@antoniopresto

@BenoitClaveau
Copy link

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

@antoniopresto
Copy link
Owner

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

@ithustle
Copy link
Contributor Author

None of these solution works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants