0

i have 2 table in mongodb. 1 is user and 2 is userbio. so now i want to take first user table and in the table on igid is store and one is object store for filters. and userbio table is store users bio. for ex user table store igid 1 so in userbio table store multiple data for igid 1. so here i want to do filter on that table and store in to final array but how can do that i have no idea any one know then please help me.

Note => In user table Filters object store 4 value. no_of_followers is string and in user bio store followerscount filed to compare. here below i have add one condition in $filter but this condition is not work propare.

This is my user table object =>

    {
        uid:595bdd5aa6ea2811d48a7802
        username:"avc"
        target_username:"afd"           
        igid:209905941
        filters{
            no_of_followres:"5000"
            no_of_following:"5"
            externalUrl:true
            privacy:false
            }
    }

This is my data in user bio table =>

     {
       username : "xyz",
       externalUrl : "xyz.com",
       fullname : "xyzh",
      isPrivate: false,
      followingCount: 49, 
      followerCount : 17,
      igid : 209905941
 },
 {
       username : "test",
       externalUrl : "",
       fullname : "test",
      isPrivate: false,
      followingCount: 49, 
      followerCount : 17,
      igid : 209905941
  },
  {
      username : "re",
      externalUrl : "",
       fullname : "re",
      isPrivate: false,
      followingCount: 49, 
      followerCount : 17,
      igid : 209905941
  },
  {
     username : "qwe",
     externalUrl : "",
       fullname : "qwe",
      isPrivate: false,
      followingCount: 49, 
      followerCount : 17,
      igid : 209905941
},
{
      username : "tre",
      externalUrl : "",
       fullname : "tre",
      isPrivate: false,
      followingCount: 49, 
      followerCount : 17,
      igid : 209905941
},
{
       username : "fdg",
       externalUrl : "",
       fullname : "dfg",
      isPrivate: false,
      followingCount: 49, 
      followerCount : 17,
      igid : 209905941
}
{
      username : "dcv",
      externalUrl : "",
       fullname : "dcv",
      isPrivate: false,
      followingCount: 49, 
      followerCount : 17,
      igid : 209905941
 },
 {
       username : "ed",
       externalUrl : "",
       fullname : "ed",
      isPrivate: false,
      followingCount: 49, 
      followerCount : 17,
      igid : 209905941
},
 {
        username : "yh",
        externalUrl : "",
       fullname : "yh",
       isPrivate: true,
      followingCount: 6000, 
      followerCount : 17,
      igid : 209905941
 }

my expected o/p =>

  var array = [{
        username : "xyz",
        externalUrl : "xyz.com",
        fullname : "xyzh",
        isPrivate: false,
        followingCount: 49, 
        followerCount : 17,
        igid : 209905941
 }]

This my query =>

 app.get(prefix + '/GetFollowersInExcel', function (req, res, next) {

    var InstaId = 209905941;

    user.aggregate([
     { $match: { igid: InstaId } },
     {
         $lookup: {
             from: "userbio",
             localField: "igid",
             foreignField: "igid",
             as: "Data"
         }
     },
      {
         $project:
              {
                  res:
                  {
                      $filter:
                      {
                          input: "$Data",
                          as: "res",
                          cond: { $lte: ["$$res.followerCount", '$user.filters.no_of_followers'] } // this is my condition
                      }
                  }
              }
     },
   ]).exec(function (err, data) {
if (err) {
    console.log(err); //res.send(err)
}
else {
    console.log("dataExel =>", data)
    //res.send(data);
}
});
  })
Edit
  • 385
  • 4
  • 24
  • So what have you already tried? You really need to show some attempt at doing it. Your question title alone would point you towards plenty of documentation on the basic process. All I see here is "please fill in this blank space with the code". That's not what this site is for – Neil Lunn Nov 16 '17 at 09:19
  • @NeilLunn i have try and write some thing like this query but using this query i can't not match my condition so can you please help me – Edit Nov 16 '17 at 10:22
  • What does "not match my condition mean". Explain in your question what exactly is wrong and what you don't understand. – Neil Lunn Nov 16 '17 at 10:24
  • You don't seem to understand that writing paragraphs is not explaining anything. Show us. Your `my expected o/p =>` does not in any way relate to what you are even attempting here. The data as a whole just looks more like copy and paste from the replication of data as well. You simply should be able to show us your current output, show what is wrong with it, what it should look like and why it should be like that. Frankly everything in the question looks like you just made it up in this post. You want to ask a real question, then show your real data and a result we can all reproduce. – Neil Lunn Nov 16 '17 at 10:32

0 Answers0