1

I am creating twitter clone using mongodb and want to search tweets which have certain hashtag in them. I came up with following document structure for my database -

{
    "_id" : ObjectId("56f88c038c297eb4048e5dff"),
    "twitterHandle" : "abhayp",
    "firstName" : "Abhay",
    "lastName" : "Pendawale",
    "emailID" : "abhayp@gmail.com",
    "password" : "278a2c36eeebde495853b14e6e5525fd12074229",
    "phoneNumber" : "12394872398",
    "location" : "San Jose",
    "birthYear" : 1992,
    "birthMonth" : 0,
    "birthDay" : 1,
    "followers" : [
        "abhayp",
        "deepakp",
        "john",
        "madhuras",
        "nupurs",
        "vgalgali"
],
"following" : [
    "abhayp",
    "abhinavk",
    "ankitac",
    "anupd",
    "arpits"        
],
"tweets" : [
    {
        "tweet_id" : "3f0fe01f8231356f784d07111efdf9d8ead28133",
        "tweet_text" : "This new twitter sounds good!",
        "created_on" : ISODate("2016-03-13T01:47:37Z"),
        "firstName" : "Abhay",
        "lastName" : "Pendawale",
        "twitterHandle" : "abhayp",
        "tags" : [ ]
    },
    {
        "tweet_id" : "4e57b6d7d6b47d69054f0be55c238e8038751d84",
        "tweet_text" : "#CMPE273 Node.js is #awesome",
        "created_on" : ISODate("2016-03-07T23:16:39Z"),
        "firstName" : "Abhay",
        "lastName" : "Pendawale",
        "twitterHandle" : "abhayp",
        "tags" : [
            "awesome",
            "CMPE273"
        ]
    },
    {
        "tweet_id" : "e5facd5f37c44313d5be02ffe0a3ca7190affd6b",
        "tweet_text" : "Getting an incredible welcome in #Pune #travel #adventure #film #india ",
        "created_on" : ISODate("2016-03-07T23:37:27Z"),
        "firstName" : "Abhay",
        "lastName" : "Pendawale",
        "twitterHandle" : "abhayp",
        "tags" : [
            "adventure",
            "film",
            "india",
            "Pune",
            "travel"
        ]
    },
    {
        "tweet_id" : "f5a735c1f747732f3e04f6cb2c092ff44750c0fd",
        "tweet_text" : "The D Day today!\n#TheDDay",
        "created_on" : ISODate("2016-03-18T22:24:57Z"),
        "firstName" : "Abhay",
        "lastName" : "Pendawale",
        "twitterHandle" : "abhayp",
        "tags" : [ ]
    }
]}

I want to find out the tweets which have "Pune" in the tags array of the tweet. I tried following command db.users.find({ "tweets.tags" : {$all: ["Pune"] }}, { tweets : 1 }).pretty();

but this command returns me ALL tweets of users who have 'Pune' entry in tags of one of their tweets. How can search only those tweets which have 'Pune' entry in their tags array?

Note: I don't want users who have tweeted #Pune, rather I want all tweets which contain #Pune. The duplicate marked question does not solve this problem.

Running following query -

    db.users.aggregate([{
    $match: {
        'tweets.tags': 'Pune'
    }
 }, {
     $project: {
         tweets: {
             $filter: {
                 input: '$tweets',
                 as: 'tweet',
                 cond: {
                     $eq: ['$$tweet.tags', 'Pune']
                 }
             }
         }
     }
 }]);

returns following results -

{ "_id" : ObjectId("56f88c038c297eb4048e5df1"), "tweets" : [ ] }
{ "_id" : ObjectId("56f88c038c297eb4048e5dff"), "tweets" : [ ] }
{ "_id" : ObjectId("56f88c038c297eb4048e5e07"), "tweets" : [ ] }

Which is certainly not what I want! :(

Neil Lunn
  • 130,590
  • 33
  • 275
  • 280
Pratik
  • 575
  • 7
  • 25
  • Hello, the duplicate question somehow does not solve my original question. Can you help me solving this question? – Pratik Mar 28 '16 at 07:05

1 Answers1

0

I don't think that's possible, beside using some convoluted aggregation stages.

The projection operator is close but only returns the first match.

My advice would be to put the tweets in a separate collection, that would be much more convenient to browse them, and probably more scalable. You're already duplicating the relevant user infos in them, so you don't have to change their content.

Ilya
  • 4,717
  • 2
  • 13
  • 29