发布于 2015-09-14 15:04:56 | 426 次阅读 | 评论: 0 | 来源: 网络整理

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.

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=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)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务