如何找到节点和mongodb? [英] How to do find in node and mongodb?

查看:111
本文介绍了如何找到节点和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"
        }

    ]
}

在用户组我试图根据 mobilenumber 获得 groupname ,我有两个组: group north group south 。获取组名后,我需要提取消息。我如何写这个来找到这个的条件。我的目标是向用户发送 group north,group south 消息。

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


推荐答案

您可以使用 $在运算符中:

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

这篇关于如何找到节点和mongodb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆