Joins in mongodb, user and his comments


#1

i have a view profile, where user details r shown
and also other members can comment on it
usercomments collection is as follows

    "_id" : "x2bchAZGGBk5shE77",
    "userid" : "bmZ4zj4LsQTfQuNuK",
    "commenttext" : "        sdfsd"
}

/* 2 */
{
    "_id" : "P59qRfAvCsMeyA7Li",
    "userid" : "bmZ4zj4LsQTfQuNuK",
    "commenttext" : "pagal panti"
}

users colleciton is

    "_id" : "bmZ4zj4LsQTfQuNuK",
    "createdAt" : ISODate("2015-08-11T08:14:23.579Z"),
    "services" : {
        "google" : {
            "accessToken" : "ya29.zwG7dacjXbE52dgG4wNghCRgpx6Y7BtJP-1FFc9vi6zr_6dS-yglu-x5DkEPMmuC3ADHNg",
            "idToken" : "eyJhbGciOiJSUzI1NiIsImtpZCI6ImQ3YThiNzVmMmE2NWQ4Yjc4MDMzZjI5ZmUyNjZhYmVhODU5MWE2YzYifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXRfaGFzaCI6IjRvSHA2TkJfaXdqaVhLTncyakRscWciLCJhdWQiOiI1ODA0MTA1MzA1OTUtN2I0Y3VlbXBqNXAzb3Z0NGhwdjUxN2x2MWRuYTNxNGguYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMDc3NDYwOTY0OTQwNTAwNDgzMTUiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXpwIjoiNTgwNDEwNTMwNTk1LTdiNGN1ZW1wajVwM292dDRocHY1MTdsdjFkbmEzcTRoLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiZW1haWwiOiJiaGF2bmFqZDkxQGdtYWlsLmNvbSIsImlhdCI6MTQzOTU0NDcxOCwiZXhwIjoxNDM5NTQ4MzE4fQ.X5BSqbGNbVQ8PTOgCOXR9flSUswJApUReIagwX4aOAhNM-oDHvPQgz6ZLhkr2HYmQYgbPmtXNdhSxO1AeL0QNN8i4bpaHm6kylzj0dHKyBwH4sYrtZFFMk3IamsdRxwBVGpVkZveq0vQGE3K8QQTTK0zUPyEGZ87VLb6pga-z5h_Uk-lRzVhezives7UJ3uvZD5ci1NQggoh1f7aXOsjNvUgi60irS6MUDqdHRtaUkwvsons-ihllTZIwkhEQxgSbD2_V3zNgPg-f8WIWPaT8_naMFqnKfMJfzQrgJ0uQNykjMeNoRh0vWUWWbQzRS7maJjOEnat_ItlAPEo2YLHtw",
            "expiresAt" : 1439548317892.0000000000000000,
            "id" : "107746096494050048315",
            "email" : "b91@gmail.com",
            "verified_email" : true,
            "name" : "Bhavna Jd",
            "given_name" : "Bhavna",
            "family_name" : "Jd",
            "picture" : "https://lh4.googleusercontent.com/-XTrSHjAevaQ/AAAAAAAAAAI/AAAAAAAACmE/2SjCQG4sfUg/photo.jpg",
            "locale" : "en",
            "gender" : "female"
        },
        "resume" : {
            "loginTokens" : [ 
                {
                    "when" : ISODate("2015-08-14T09:31:59.043Z"),
                    "hashedToken" : "YKd+0AyfWqIf6g5I8PmydkS2hsLOQ/qknGUBJUhbeLQ="
                }
            ]
        }
    },
    "profile" : {
        "name" : "Bhavna Jd",
        "location" : {
            "lat" : 19.2158726999999985,
            "lng" : 73.0957001000000020
        },
        "image" : "/cfs/files/images/6QEotuGhSWob28sBq",
        "gender" : "",
        "countryname" : "india",
        "state" : "maharashtra",
        "city" : "mumbai",
        "aboutme" : "hello world",
        "mobile" : "1234",
        "bdate" : ISODate("2015-08-13T18:30:00.000Z"),
        "education" : "bcome",
        "annualIncome" : "no income",
        "profession" : "worker",
        "cgoal" : "no goals have fun",
        "motherToung" : "kannadi",
        "religion" : "hindu",
        "caste" : "marathi",
        "father" : "father working",
        "mother" : "house wife",
        "brothernsister" : "no brothers and sisters",
        "pets" : "i love dog",
        "lookingfor" : "boy friend",
        "purpose" : "Friendship",
        "starsign" : "Aquarius",
        "maritalstatus" : "Never Married",
        "height" : "5.2(1.58 mts)",
        "eyesight" : "Clear",
        "weight" : "Slim",
        "complexion" : "Fair",
        "haircolor" : "Black",
        "smoke" : "Non-Smoker",
        "drink" : "Dont Drink",
        "diet" : "Non-Veg",
        "drugs" : "No Drugs"
    },
    "registered_emails" : [ 
        {
            "address" : "b91@gmail.com",
            "verified" : true
        }
    ],
    "status" : {
        "online" : true,
        "lastLogin" : {
            "date" : ISODate("2015-08-14T10:42:31.980Z"),
            "ipAddr" : "127.0.0.1",
            "userAgent" : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36"
        },
        "idle" : false
    }
}``` 

now how do i display that username and also wants to post his image besides comment

i have written 1 helper
'
Template.usercommentstemp.helpers({
    uzercomments:function(){
        // alert(this._id);
        return Usercomments.find({userid:this._id});
    },
    uzernames:function(){
       return Meteor.users.find({}, {name:1, _id:this._id});


    }
});'

but uzernames return object
what can i do about this

#2

Something like this?

uzernames:function(){
  return Meteor.users.findOne(this.id, { fields: { 'profile.name': 1 } }).profile.name;
}

#3

thans steve for replying
but its not working
it says profile is undefined
Cannot read property ‘profile’ of undefined

profile is there in collection and name is also correct


#4

can u throw some light on this

but iam not good at syntax it shows

can u just have look at it


#5

Should this be:

uzernames:function(){
  return Meteor.users.findOne(this._id, { fields: { 'profile.name': 1 } }).profile.name;
}

So _id not id.


#6

it still gives the error
Cannot read property ‘profile’ of undefined


#7

You missed the important part of the change: try using _id, not id.


#8

did that
here is the code iam pasting, i have also subscribed to users and usercomments

'Template.usercommentstemp.helpers({
uzercomments:function(){
alert(this._id);
return Usercomments.find({userid:this._id});
},
uzernames:function(){
//alert(Meteor.users.findOne({ _id:this._id}, { fields: { ‘profile.name’: 1 } })).profile.name;
return Meteor.users.findOne({ _id:this._id}, { fields: { ‘profile.name’: 1 } }).profile.name;
}

})
'
underscore is not visible


#9

So if you do a console.log(this) at the top of your uzernames function:

uzernames:function(){
  console.log(this);
  return Meteor.users.findOne({_id:this.id}, { fields: { 'profile.name': 1 } }).profile.name;
}

what do you get?

Please paste the response, select it and click the </> button on the post editor before submitting. It’s very hard to read unformatted code.


#10

the package reywood:publish-composite is nice. Why not just publish the data you need? It looks like you’re on someone’s profile page.

Meteor.publishComposite('user_comments', function (userId) {
  return {
    find: function () {
      return Meteor.users.find(userId, {
        fields: { 'profile.name': 1 }
      });
    },
    children: [{
      find: function (user) {
        return Comments.find({ userId: user._id });
      }
    }]
  }
});

#11

Change id:this.id to _id:this._id, also make sure you are publishing the necessary user data, otherwise, it would end up undefined. @corvid’s suggestion to use reywood:publish-composite is a very good choice.


#12

my 2 cents: it will always return as undefined, if you dont wait for user collection to be available before executing it.
so use fast-render or wait for subscription and than execute function.


#13

you can check it easily, for example execute that command in browser console and debug it there.

And after is working in console, use it in app.
If it is working in console and not app, than you have timing issue.

So try in console.

Meteor.users.findOne({_id: "x2bchAZGGBk5shE77" }, { fields: { 'profile.name': 1 } }).profile.name;

#14

i finally decided to denormalize data joins wont work properly
reywood publishcomposite didnt work and i suppose it will rerender and reender and will slow down my app

for simple user profiles, people can comment
so i will just add name,userid, profileimage, and comment field
in comment collection