0

I want to get data in nodejs application as describe bellow out put.
user.js, userProfile are the models of mongoose.

user.js

    var userSchema = new Schema({
         nick_name:{type:String},
         email: {type: String},
         password: {type: String},
        is_active:{type:String,enum:['1','0'],default:"1"},
    },{ collection: 'user'});

userProfile.js

    var userProfileSchema = new Schema({
        user_id:{type:Schema.Types.ObjectId, ref:'User'},
        first_name:{type:String},
        last_name:{type:String},
        address:{type:String},
        country:{type:String},
        state:{type:String},
        city:{type:String},
        gender:{type:String,enum:['m','f','o'],default:"m"},
        is_active:{type:String,enum:['1','0'],default:"1"},
    },{ collection: 'userProfile'});

wants to get out put as follows

    {
     "nick_name"   : "laxman",
     "email"       : "laxman@mailinator.com",
     "first_name"  : "my first name",
     "last_name"   : "my last name",
     "address"     : "my address",
     "country"     : "my country",
     "state"       : "my state",
     "city"        : "my city",
     "gender"      : "m",
    }

dependencies

    "express"  => "version": "4.7.4",
    "mongoose" => "version": "4.4.5",
    "mongodb"  => "version": "2.4.9",
    "OS"  => "ubuntu 14.04 lts 32bit",

SQL query for reference

SELECT * FROM `user`  
       left join `user_profile` 
       on user.id =user_profile.user_id 
WHERE 1
laxman
  • 1,338
  • 2
  • 10
  • 27
  • you have to use map-reduce functionality of MongoDB. Here are some ref links . [Example](https://www.noppanit.com/merge-documents-two-collections-together-mongodb) [StackOverflow](http://stackoverflow.com/questions/5681851/mongodb-combine-data-from-multiple-collections-into-one-how) [MongoDB](https://docs.mongodb.org/manual/core/map-reduce/#MapReduce-Outputoptions) – Sarju Mar 07 '16 at 11:04
  • how to use it in mongoose – laxman Mar 10 '16 at 06:43

1 Answers1

0

The answer to this question is going to depend on how you are doing your authentication, but I will answer this question how I would achieve what you are asking.

Firstly, I would make one Schema instead of having two separate ones. Then, depends on your authentication method, I use tokens, you will pass that data to whatever you is receiving it on the client side. It would also help to know if you are using anything on the client side that handles HTTP requests or if this is just a node application.

I'll try to cover both methods in some detail.

Say you are using cookies for instance, I'm going to assume you know how to set up a session using Express, or whatever if any framework you are using on top of node, as well as assuming you know what you are doing as far as securing your passwords properly and skip straight to the api building.

Pretend this is a controller on your server side to authenticate the user on login.

var User = mongoose.model('User');

exports.login = function (req, res) {
    User.findOne({email: req.body.email), function (err, user) {
        if (err) {
            return res.json(err);
        } else if (!user) {
            return res.json({message: 'User not found.'});
        } else {
          if (req.body.password === user.password) {
              req.session.user = user;
          } else {
            return res.json({message: 'Invalid username or password.'});
          }
        }
      )};
     };

Of course, you want to make sure you have some middleware set up so the password is definitely not send along with the user, but that is not the matter here.

Once you have that, then any other route where you need user data is fairly simple. You make another controller like so:

exports.profile = function (req, res) {
 if (req.session && req.session.user) {
    User.findOne({email: req.session.user.email}, function (err, user) {
      if (!user) {
          res.session.reset();
          res.redirect('/login');
      } else {
        res.locals.user = user;
        res.render('/profile);
      }
    )};
   }
  };

I did the rendering on the server side, but if you are using something like Angular, you can pull that to the client side.

If you are using tokens, which I highly recommend, then the method is very similar. I will be happy to explain some of that as well.

bashkir
  • 11
  • 4