Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
How to do find in node and mongodb?

This is my 'usergroups' data

{
    "_id": {
        "$oid": "58f7537ec422895572e988a1"
    },
    "name": "aaa",
    "groupname": "group north,group south",
    "mobilenumber": "0509867865",
    "userid": "6035555c16"

}

here my 'groups' collection data's

{
    "_id": {
        "$oid": "58e5eb3c035555c33f979daf"
    },
    "groupname": "group north",
    "message": [
        {
            "val": "hai how are you",
            "key": "1"
        },
        {
            "val": "i am fine",
            "key": "2"
        }

    ]
},
{
    "_id": {
        "$oid": "3c035555c33f979daf58e5eb"
    },
    "groupname": "group south",
    "message": [
        {
            "val": "testing",
            "key": "1"
        }

    ]
},
{
    "_id": {
        "$oid": "55c3c035533f979f58e5ebda"
    },
    "groupname": "group east",
    "message": [
        {
            "val": "where are you from",
            "key": "1"
        }

    ]
}

In Usergroup I am trying to get the groupname based on mobilenumber and I have two groups: group north and group south. After getting groupname I need to fetch the message. How can I write this to find the condition for this. My goal is to send group north, group south message to the user.

Usergroup.findOne({
            mobilenumber: 0509867865
        },
        function(err, usergroup) {
            if (err) {
                return handleError(res, err);
            }
            if (!usergroup) {
                return res.status(404).send('Not Found');
            }
            console.log(usergroup.groupname);
        //group north ,group south
            Group.find({
                    groupname: 
                },
                function(err, group) {
                    if (err) {
                        return handleError(res, err);
                    }

                    return res.json(group);
                });
        });
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use the $in operator:

Usergroup.findOne({
            mobilenumber: 0509867865
        },
        function(err, usergroup) {
            if (err) {
                return handleError(res, err);
            }
            if (!usergroup) {
                return res.status(404).send('Not Found');
            }
            console.log(usergroup.groupname);
        //group north ,group south
            Group.find({
                    groupname: { $in: usergroup.groupname.split(",") } 
                },
                function(err, group) {
                    if (err) {
                        return handleError(res, err);
                    }

                    return res.json(group);
                });
        });
about 4 years ago · Santiago Trujillo Report

0

You can use the below aggregation for 3.4 version.

The query will $match documents in the usergroups collection with the mobilenumber followed by $split to split the groupnames and $lookup the groupname in the groups collection for messages. Final step is to $unwind the mobile_group array to $project the messages for each groupname.

db.usergroups.aggregate([
    { $match: { mobilenumber:"0509867865" } },
    { $project: { groupname:{ $split: [ "$groupname", ',' ] }  } },
    { $lookup:
        {
          from: "groups",
          localField: "groupname",
          foreignField: "groupname",
          as: "mobile_group"
        }
   },
   {$unwind:"$mobile_group"},
   { $project : { groupname:"$mobile_group.groupname", "messages" : "$mobile_group.message" } }
])
about 4 years ago · Santiago Trujillo Report

0

You can simply get the groupname from the Usergroup, split it to array of two group name using str.split(',') and then query the Group for two groupname as follow:

    Usergroup.findOne({ mobilenumber: 0509867865 }, function(err, usergroup) {
        if (err) {
            return handleError(res, err);
        }
        if (!usergroup) {
            return res.status(404).send('Not Found');
        }
        console.log(usergroup.groupname);

        var groupname   = usergroup.groupname;
        var groups      = groupname.split(',');
        var messages    = {};

        for (var i = groups.length - 1; i >= 0; i--) {
            Group.find({ groupname: groups[i] }, function(err, group) {
                if (err) {
                    return handleError(res, err);
                }
                messages[group.groupname] = group.message;
            });
            if (i == 0) {
                return res.status(200).json(messages);
            }
        }

    });
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!