SQL to mongo Shell to C++
2016-01-29 18:05
411 查看
转载自:
http://mongodb-documentation.readthedocs.org/en/latest/ecosystem/drivers/cpp-to-sql-to-mongo-shell.html#sql-to-mongo-shell-to-cpp
Several of the C++ driver methods throw mongo::DBException, so you will want
a try/catch statement as some level in your program. Also be sure to callc.getLastError() after
writes to check the error code.
http://mongodb-documentation.readthedocs.org/en/latest/ecosystem/drivers/cpp-to-sql-to-mongo-shell.html#sql-to-mongo-shell-to-cpp
Several of the C++ driver methods throw mongo::DBException, so you will want
a try/catch statement as some level in your program. Also be sure to callc.getLastError() after
writes to check the error code.
SQL | mongo Shell | C++ Driver |
---|---|---|
INSERT INTO USERS VALUES( 1, 1) | db.users.insert( { a: 1, b: 1 } ) | // GENOID is optional. if not done by client, // server will add an _id c.insert("mydb.users", BSON(GENOID<<"a"<<1<<"b"<<1)); // then: string err = c.getLastError(); |
SELECT a,b FROM users | db.users.find( {}, {a: 1, b: 1 } ) | auto_ptr<DBClientCursor> cursor = c.query("mydb.users", Query(), 0, 0, BSON("a"<<1<<"b"<<1)); |
SELECT * FROM users | db.users.find() | auto_ptr<DBClientCursor> cursor = c.query("mydb.users", Query()); |
SELECT * FROM users WHERE age=33 | db.users.find( { age: 33 } ) | auto_ptr<DBClientCursor> cursor = c.query("mydb.users", QUERY("age"<<33)) // or: auto_ptr<DBClientCursor> cursor = c.query("mydb.users", BSON("age"<<33)) |
SELECT * FROM users WHERE age=33ORDER BY name | db.users.find( { age: 33 } ).sort( { name: 1 } ) | auto_ptr<DBClientCursor> cursor = c.query("mydb.users", QUERY("age"<<33).sort("name")); |
SELECT * FROM users WHERE age>33 AND age<=40 | db.users.find( { 'age': { $gt:33, $lte:40 } } ) | auto_ptr<DBClientCursor> cursor = c.query("mydb.users", QUERY("age"<<GT<<33<<LTE<<40)); |
CREATE INDEX myindexname ON users(name) | db.users.ensureIndex( {name: 1 } ) | c.ensureIndex("mydb.users", BSON("name"<<1)); |
SELECT * FROM users LIMIT 10 SKIP 20 | db.users.find().limit(10).skip(20) | auto_ptr<DBClientCursor> cursor = c.query("mydb.users", Query(), 10, 20); |
SELECT * FROM users LIMIT 1 | db.users.findOne() | bo obj = c.findOne("mydb.users", Query()); |
SELECT DISTINCT last_name FROM users WHERE x=1 | db.users.distinct( 'last_name', {x: 1} ) | // no helper for distinct yet in c++ driver, // so send command manually bo cmdResult; bool ok = c.runCommand( "mydb", BSON("distinct" << "users" << "key" << "last_name" << "query" << BSON("x"<<1)), cmdResult); list<bo> results; cmdResult["values"].Obj().Vals(results); |
SELECT COUNT(*) FROM users where AGE > 30 | db.users.find( { age: { $gt: 30 } } ).count() | unsigned long long n = c.count("mydb.users", BSON("age"<<GT<<30)); |
UPDATE users SET a=a+2 WHERE b='q' | db.users.update( { b: 'q' }, { $inc: { a:2 } }, false, true) | c.update("mydb.users", QUERY("b"<<"q"), BSON("$inc"<<BSON("a"<<2)), false, true); // then optionally: string err = c.getLastError(); bool ok = err.empty(); |
DELETE FROM users WHERE z="abc" | db.users.remove( { z: 'abc' } ) | c.remove("mydb.users", QUERY("z"<<"abc")); // then optionally: string err = c.getLastError(); |
相关文章推荐
- 【转载】MySQL被慢sql hang住了,用shell脚本快速清除不断增长的慢sql的办法
- shell特殊命令符
- shell特殊命令符
- shell特殊命令符
- shell特殊命令符
- shell特殊命令符
- shell特殊命令符
- shell特殊命令符
- shell特殊命令符
- shell特殊命令符
- shell特殊命令符
- 16. Shell scripts
- shell 练习
- shell编程-变量
- Linux Shell概述
- shell编程实例2
- Shell文件包含
- Shell输入输出重定向:Shell Here Document,/dev/null文件
- Shell函数参数
- Shell函数:Shell函数返回值、删除函数、在终端调用函数