gpt4 book ai didi

c++ - mongodb c++ 驱动程序不更新

转载 作者:行者123 更新时间:2023-11-28 06:45:09 25 4
gpt4 key购买 nike

在 mongodb 中,我有这个文档:

{
"_id" : ObjectId("53e157918b3354f7157142d2"),
"packet" : [
{
"datetime" : ISODate("2014-08-05T16:16:46.337Z"),
"signallingType" : "M2PA",
"clgNum" : "",
"cldNum" : "",
"opc" : "6406",
"dpc" : "327",
"transState" : "continue",
"otid" : "M2PA042f2ee0",
"dtid" : "",
"sccpCalling" : "523331461111",
"sccpCalled" : "52333033342222",
"imsi" : "",
"operation" : "sendAuthenticationInfo (56)",
"camelClgNum" : "",
"camelCldNum" : "",
"camelCallRefNum" : "",
"camelImsi" : "",
"camelEvent" : "",
"camelReleaseCause" : "",
"pcapFileName" : "/home/monitor/tmp/gsm_eth2_06700_20140805165712.pcap",
"message" : "dddvid"
}
]

如果我从 shell 执行以下命令:

db.packets.update( 
{ $and: [
{ $or: [
{ "packet.otid": { $in: [ "M2PA042f2ee0" ] } },
{ "packet.dtid": { $in: [ "M2PA042f2ee0" ] } }
] },
{ $or: [
{ "packet.sccpCalling": { $in: [ "523331461111", "52333033342222" ] } },
{ "packet.sccpCalled": { $in: [ "523331461111", "52333033342222" ] } }
] }
] },
{ $push: { packet: { datetime: new Date(1407255406337) } } }, true
)

文档被毫无问题地更新。现在,使用 C++,我可以:

class Packet {

public:
std::string packetDate;
std::string signallingType;
int creationTimeStamp;
std::string clgNum;
std::string cldNum;
std::string opc;
std::string dpc;
std::string transState;
std::string otid;
std::string dtid;
std::string message;
std::string sccpCalling;
std::string sccpCalled;
std::string imsi;
std::string operation;
std::string camelClgNum;
std::string camelCldNum;
std::string camelCallRefNum;
std::string camelImsi;
std::string camelEvent;
std::string camelReleaseCause;
std::string pcapFile;
...

我被要求展示如何创建“query_otid_or”和 query_sccp_or“:

mongo::BSONObj query;
mongo::BSONObj objError;
mongo::BSONObj query_otid;
mongo::BSONObj query_dtid;
mongo::BSONObj query_sccpCalling;
mongo::BSONObj query_sccpCalled;
mongo::BSONObj query_otid_or;
mongo::BSONObj query_sccp_or;
...
tcapValuesArray.clear(); sccpValuesArray.clear();
if( (it)->otid.length() > 0 ){
tcapValuesArray.push_back( (it)->otid );
}

if( (it)->dtid.length() > 0 ){
tcapValuesArray.push_back( (it)->dtid );
}

if( (it)->sccpCalling.length() > 0 ){
sccpValuesArray.push_back( (it)->sccpCalling );
}
if( (it)->sccpCalled.length() > 0 ){
sccpValuesArray.push_back( (it)->sccpCalled );
}

query_otid = BSON( "\"packet.otid\"" << BSON( "$in" << tcapValuesArray ) );
query_dtid = BSON( "\"packet.dtid\"" << BSON( "$in" << tcapValuesArray ) );

query_sccpCalling = BSON( "\"packet.sccpCalling\"" << BSON( "$in" << sccpValuesArray ) );
query_sccpCalled = BSON( "\"packet.sccpCalled\"" << BSON( "$in" << sccpValuesArray ) );

query_otid_or = mongo::OR( query_otid, query_dtid );
query_sccp_or = mongo::OR( query_sccpCalling, query_sccpCalled );

...

c.update("tracer.packets",
BSON( "$and" << BSON_ARRAY( query_otid_or << query_sccp_or ) ),
BSON( "$push" << BSON("packet" << BSON( "datetime" << mongo::Date_t( atoll( ((it)->packetDate.substr(0,10) + (it)->packetDate.substr(11,3)).c_str() ))))),
true,
true);

它不起作用,插入了一条新记录...尽管它是完全相同的命令...

有人有什么想法吗??

最佳答案

需要说明的是,在您的第一个示例(通过 shell)中,运行 upsert 的结果是文档在查询匹配时得到更新。

它对我来说似乎工作正常,我认为你过度引用了 "\"packet.sccpCalling\"""\"packet.sccpCalled\"" .

#include <iostream>
#include <vector>
#include <cassert>
#include "mongo/client/dbclient.h"

using namespace std;
using namespace mongo;

int main() {
client::initialize();

DBClientConnection conn;
conn.connect("localhost");
conn.dropDatabase("test");

BSONObjBuilder bob;
BSONArrayBuilder bab;
BSONObjBuilder packet;

packet.append("otid", "M2PA042f2ee0");
packet.append("sccpCalling", "523331461111");
packet.append("sccpCalled", "52333033342222");

bab.append(packet.obj());
bob.appendArray("packet", bab.obj());
conn.insert("test.test", bob.obj());

vector<std::string> tcapValuesArray;
tcapValuesArray.push_back("M2PA042f2ee0");
vector<std::string> sccpValuesArray;
sccpValuesArray.push_back("523331461111");
sccpValuesArray.push_back("52333033342222");

BSONObj query_otid = BSON( "packet.otid" << BSON( "$in" << tcapValuesArray ) );
BSONObj query_sccpCalling = BSON( "packet.sccpCalling" << BSON( "$in" << sccpValuesArray ) );
BSONObj query_sccpCalled = BSON( "packet.sccpCalled" << BSON( "$in" << sccpValuesArray ) );

BSONObj query_otid_or = OR( query_otid, query_otid );
BSONObj query_sccp_or = OR( query_sccpCalling, query_sccpCalled );

BSONObj query = BSON( "$and" << BSON_ARRAY( query_otid_or << query_sccp_or ) );
BSONObj update = BSON( "$push" << BSON("packet" << BSON( "datetime" << "updated")));

cout << "this is the query: " << query.toString() << endl;
conn.update("test.test", query, update, true, true);

assert( 1 == conn.count("test.test"));
assert( 1 == conn.count("test.test", Query("{ 'packet.datetime': 'updated' }")));
}

关于c++ - mongodb c++ 驱动程序不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25149384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com