How to do find in node and mongodb?

前端 未结 3 727
无人共我
无人共我 2021-01-22 22:25

This is my \'usergroups\' data

{
    \"_id\": {
        \"$oid\": \"58f7537ec422895572e988a1\"
    },
    \"name\": \"aaa\",
    \"groupname\": \"group north,gro         


        
3条回答
  •  太阳男子
    2021-01-22 22:48

    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);
                }
            }
    
        });
    

提交回复
热议问题