-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchange_type.cpp
More file actions
122 lines (96 loc) · 3.49 KB
/
change_type.cpp
File metadata and controls
122 lines (96 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* 更改MongoDB数据库文档中原有字段的数据类型
* 先获取文档 BSONObj
* 在获取字段 BSONElement
* 判断原字段类型 BSONElement.type()
* 与 BSONType 做比较
*/
// system
// std
#include <string>
#include <vector>
#include <iostream>
// mongodb
#include "mongo/client/dbclient.h"
// boost
#include <boost/lexical_cast.hpp>
using namespace std;
int main( int argc, const char **argv )
{
string errmsg;
mongo::DBClientConnection conn;
// mongo数据库地址
const char host[] = "10.0.110.13:27017";
// 文档集合
const char ns[] = "termcloud.log";
if ( ! conn.connect(host , errmsg ) )
{
cout << "couldn't connect : " << errmsg << endl;
return EXIT_FAILURE;
}
// 指定文档类型
vector<string> actions;
actions.push_back("library_uncollect");
actions.push_back("library_collect");
actions.push_back("library_share");
actions.push_back("library_unsharedo");
for (std::vector<string>::iterator it = actions.begin() ; it != actions.end(); ++it)
{
std::cout << "Query Action Type is " << *it << endl;
// query
// 根据文档类型遍历文档
auto_ptr<mongo::DBClientCursor> cursor = conn.query(ns,
QUERY("action_type" << (*it).c_str()));
if (!cursor.get())
{
cout << "query error" << endl;
return EXIT_FAILURE;
}
string str_lib_id;
int create_time = 0;
int library_id = 0;
while ( cursor->more() )
{
cout << endl;
cout << endl;
cout << "has element, action type : " << *it << "." << endl;
mongo::BSONObj obj = cursor->next();
try
{
mongo::BSONElement element = obj.getFieldDotted("library_id");
if(element.type() == mongo::NumberInt)
{
// NumberInt
// cout << "Well " << endl;
}
else if(element.type() == mongo::String)
{
// String
str_lib_id = obj["library_id"].String();
create_time = obj["create_time"].Int();
cout << "Query Mongo:" << endl;
cout << "library_id =" << str_lib_id << endl;
cout << "create_time =" << create_time << endl;
library_id = boost::lexical_cast<int>(str_lib_id);
conn.update( ns ,
mongo::BSONObjBuilder().append( "create_time" , create_time).obj() , BSON("$set"<<BSON("library_id"<<library_id)));
cout << "Done! " << *it << endl;
}
}
catch(mongo::MsgAssertionException &e)
{
cout << "error : mongo::obj['library_id'].String(), library_id=" << endl;
cout << "----------------------------------------------------------------------------" << endl;
cout << obj.jsonString() << endl;
cout << "----------------------------------------------------------------------------" << endl;
cout << "error : " << e.what() << endl;
continue;
}
catch(...)
{
cout << "error : boost::lexical_cast<int>, library_id=" << str_lib_id << endl;
continue;
}
}
}
return 0;
}