posts - 6,comments - 0,trackbacks - 0

MongoDB的查询表示为JSON(BSON)对象。此快速参考图表显示了作为SQL的例子,Mongo shell 语法,Mongo的C + +驱动程序的语法。

MongoDB的(和其他的东西,比如一个索引键模式)中的查询表达式表示为BSON。在C + +中,你可以使用BSONObjBuilder(又名 bson::bob )建立BSON对象,或BSON()宏。下面的例子假设已经建立的连接C:

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

几个在C + +驱动方法抛出的mongo::DBException ,所以你会想一些在你的程序水平作为一个try / catch语句。

 

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 optionally:
            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(*y) FROM users where AGE > 30
db.users.find({age: {'$gt': 30}}).count()
unsigned long long n = c.count("mydb.users", QUERY("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();

posted on 2012-06-11 21:27 意吟 阅读(1395) 评论(0)  编辑 收藏 引用 所属分类: MongoDB

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理