skip to Main Content
    {
     "entities" : {
            "hashtags" : [
                    {
                            "text" : "NightToInspire",
                            "indices" : [
                                    38,
                                    53
                            ]
                    }
            ],
            "symbols" : [ ],
            "user_mentions" : [
                    {
                            "screen_name" : "AFC_Foundation",
                            "name" : "TheArsenalFoundation",
                            "id" : 570099076,
                            "id_str" : "570099076",
                            "indices" : [
                                    20,
                                    35
                            ]
                    },
                    {
                            "screen_name" : "Arsenal",
                            "name" : "Arsenal FC",
                            "id" : 34613288,
                            "id_str" : "34613288",
                            "indices" : [
                                    57,
                                    65
                            ]
                    }
            ]

        }

How would I go about querying this nested JSON array(subset of what was actually returned) from the twitter api to get both screen_name’s that appear in the array. Using a db.collection.find(“something”) format

2

Answers


  1. Something like this may help,

    var entities = db.Entities.find();
    
    entities.user_mentions.each(function(err, item) {
              console.log(item.screen_name)  // do whatever  
     }
    
    });
    
    Login or Signup to reply.
  2. Not sure if you absolutely need to use find() but aggregate would work:

        db.Entities.aggregate([{$unwind : '$entities.user_mentions'},{$project : {'screen_name':'$entities.user_mentions.screen_name'}}])
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search