SQL && mongo Shell && C++ Driver

本文总结了几种常用的SQL语句对应的mongo Shell及c++语法。

MongoDB queries are expressed as JSON (BSON) objects. This quick reference chart shows examples as SQL, mongo shell syntax, and MongoDB C++ driver syntax.

A query expression in MongoDB (and other things, such as an index key pattern) is represented as BSON. In C++ you can use BSONObjBuilder (aka bson::bob) to build BSON objects, or the BSON() macro. The examples below assume a connection c already established:

using namespace bson;
DBClientConnection c;
c.connect("somehost");

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 call c.getLastError() after writes to check the error code.

SQLmongo ShellC++ 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 usersdb.users.find( {},
               {a: 1, b: 1 }
             )
auto_ptr<DBClientCursor> cursor =
  c.query("mydb.users", Query(),
  0, 0, BSON("a"<<1<<"b"<<1));
SELECT * FROM usersdb.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=33
ORDER 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();
   
   
   
   
   
   


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值