SQL
|
Mongo Shell
|
C++ Driver |
INSERT INTO USERS VALUES(1,1) |
db.users.insert({a:1,b:1}) |
c.insert("mydb.users", BSON(GENOID<<"a"<<1<<"b"<<1));
string err = c.getLastError(); |
|
db.users.find({}, {a:1,b:1}) |
auto_ptr<DBClientCursor> cursor =
c.query("mydb.users", Query(), 0, 0, BSON("a"<<1<<"b"<<1));
|
|
|
auto_ptr<DBClientCursor> cursor =
c.query("mydb.users", Query());
|
SELECT * FROM users WHERE age=33 |
|
auto_ptr<DBClientCursor> cursor =
c.query("mydb.users", QUERY("age"<<33))
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 |
|
bo obj = c.findOne("mydb.users", Query()); |
SELECT DISTINCT last_name FROM users WHERE x=1 |
db.users.distinct('last_name',{x:1}) |
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);
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"));
string err = c.getLastError(); |