0

I want to query Mongodb: find all users, that have 'artist'=Iowa in any array item of objects. Here is Robomongo of my collection: mongo_find_music In Python I'm doing:

Vkuser._get_collection().find({
  'member_of_group': 20548570,
  'my_music': {
    'items': {
      '$elemMatch': {
        'artist': 'Iowa'
      }
    }
  }
}) 

but this returns nothing. Also tried this: {'member_of_group': 20548570, 'my_music': {'$elemMatch': {'$.artist': 'Iowa'}}} and that didn't work.

Here is part of document with array:

    "can_see_audio" : 1,
"my_music" : {
    "items" : [ 
        {
            "name" : "Anastasia Plotnikova",
            "photo" : "http://cs620227.vk.me/v620227451/9c47/w_okXehPbYc.jpg",
            "id" : "864451",
            "name_gen" : "Anastasia"
        }, 
        {
            "title" : "Ain't Talkin' 'Bout Dub",
            "url" : "http://cs4964.vk.me/u14671028/audios/c5b8a0735224.mp3?extra=jgV4ZQrFrsfxZCJf4gsRgnKWvdAfIqjE0M6eMtxGFpj2yp4vjs5DYgAGImPMp4mCUSUGJzoyGeh2Es6L-H51TPa3Q_Q",
            "lyrics_id" : 24313846,
            "artist" : "Apollo 440",
            "genre_id" : 18,
            "id" : 344280392,
            "owner_id" : 864451,
            "duration" : 279
        }, 
        {
            "title" : "Animals",
            "url" : "http://cs1316.vk.me/u4198685/audios/4b9e4536e1be.mp3?extra=TScqXzQ_qaEFKHG8trrwbFyNvjvJKEOLnwOWHJZl_cW5EA6K3a9vimaMpx-Yk5_k41vRPywzuThN_IHT8mbKlPcSigw",
            "lyrics_id" : 166037,
            "artist" : "Nickelback",
            "id" : 344280351,
            "owner_id" : 864451,
            "duration" : 186
        }, 
1
  • 3
    You know that the main reason there is a restriction on "low reputation users" here on posting images in their questions is because generally speaking "An image is not what we want to see, it's 'data'" which you can basically show as text. So it kind of helps people if you both "show data as text" and also with something valid towards the question you are asking. Nothing here currently matches the conditions you are asking for, Commented Mar 4, 2015 at 13:25

1 Answer 1

1

The following query should work. You can use the dot notation to query into sub documents and arrays.

Vkuser._get_collection().find({
  'member_of_group': 20548570,
  'my_music.items.artist':'Iowa'
})

The following query worked for me in the mongo shell

db.collection1.find({ "my_music.items.artist" : "Iowa" })
Sign up to request clarification or add additional context in comments.

Comments

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.