程序记录1(添加字段)

bool DBCenter::setUserTableTest()
{
    //修改user表
    UseTable(m_userTable);
    for (int i = 999; i < 1000; ++i)
    {
        char condJson[JSON_LENGTH_MIN] = {0};
        sprintf(condJson, "{%s:%d}", m_userStruct._id.c_str(), i);
        char secJson[JSON_LENGTH_MIN] = {0};
        sprintf(secJson, "{%s:1}", m_userStruct.aids[0].c_str());
        char *Res = NULL;
        bool isExit;
        char *errMessage = NULL;
        m_mongo->FindOneData(m_ns.c_str(), condJson, secJson, &Res, &isExit, m_errMsg);
        if (!isExit)
        {
            continue;
        }
        if(!strstr(Res, m_userStruct.aids[0].c_str()))//没有角色
            continue;
        //获取角色的个数
        using namespace boost::property_tree;
        ptree proot,//树根
            pchild,//子树
            pnode;//节点
        stringstream stream(Res);
        //将流放入树根
        read_json<ptree>(stream,proot);
        pchild=proot.get_child(m_userStruct.aids[0].c_str());
        for (int i = 0; i < pchild.size(); i++)
        {
            char setJson[JSON_LENGTH_MIN] = {0};
            sprintf(setJson, "{$set:{'%s.%d.%s':0}}", m_userStruct.aids[0].c_str(), i, m_userStruct.aids[4].c_str());
            if (!m_mongo->UpdataData(m_ns.c_str(), condJson, setJson))
            {
                return false;
            }
        }
    }
    return true;
}
原文地址:https://www.cnblogs.com/zzyoucan/p/3654543.html