summaryrefslogtreecommitdiff
path: root/client/examples
diff options
context:
space:
mode:
authorAntonin Kral <a.kral@bobek.cz>2010-01-31 08:32:52 +0100
committerAntonin Kral <a.kral@bobek.cz>2010-01-31 08:32:52 +0100
commit4eefaf421bfeddf040d96a3dafb12e09673423d7 (patch)
treecb2e5ccc7f98158894f977ff131949da36673591 /client/examples
downloadmongodb-4eefaf421bfeddf040d96a3dafb12e09673423d7.tar.gz
Imported Upstream version 1.3.1
Diffstat (limited to 'client/examples')
-rw-r--r--client/examples/authTest.cpp53
-rw-r--r--client/examples/clientTest.cpp214
-rw-r--r--client/examples/first.cpp85
-rw-r--r--client/examples/second.cpp56
-rw-r--r--client/examples/tail.cpp55
-rw-r--r--client/examples/tutorial.cpp67
-rw-r--r--client/examples/whereExample.cpp68
7 files changed, 598 insertions, 0 deletions
diff --git a/client/examples/authTest.cpp b/client/examples/authTest.cpp
new file mode 100644
index 0000000..77ce12d
--- /dev/null
+++ b/client/examples/authTest.cpp
@@ -0,0 +1,53 @@
+// authTest.cpp
+
+/* Copyright 2009 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+
+#include "client/dbclient.h"
+
+using namespace mongo;
+
+int main( int argc, const char **argv ) {
+
+ const char *port = "27017";
+ if ( argc != 1 ) {
+ if ( argc != 3 )
+ throw -12;
+ port = argv[ 2 ];
+ }
+
+ DBClientConnection conn;
+ string errmsg;
+ if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
+ cout << "couldn't connect : " << errmsg << endl;
+ throw -11;
+ }
+
+ { // clean up old data from any previous tests
+ conn.remove( "test.system.users" , BSONObj() );
+ }
+
+ conn.insert( "test.system.users" , BSON( "user" << "eliot" << "pwd" << conn.createPasswordDigest( "eliot" , "bar" ) ) );
+
+ errmsg.clear();
+ bool ok = conn.auth( "test" , "eliot" , "bar" , errmsg );
+ if ( ! ok )
+ cout << errmsg << endl;
+ assert( ok );
+
+ assert( ! conn.auth( "test" , "eliot" , "bars" , errmsg ) );
+}
diff --git a/client/examples/clientTest.cpp b/client/examples/clientTest.cpp
new file mode 100644
index 0000000..bbb82f6
--- /dev/null
+++ b/client/examples/clientTest.cpp
@@ -0,0 +1,214 @@
+// clientTest.cpp
+
+/* Copyright 2009 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * a simple test for the c++ driver
+ */
+
+#include <iostream>
+
+#include "client/dbclient.h"
+
+using namespace std;
+using namespace mongo;
+
+int main( int argc, const char **argv ) {
+
+ const char *port = "27017";
+ if ( argc != 1 ) {
+ if ( argc != 3 )
+ throw -12;
+ port = argv[ 2 ];
+ }
+
+ DBClientConnection conn;
+ string errmsg;
+ if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
+ cout << "couldn't connect : " << errmsg << endl;
+ throw -11;
+ }
+
+ const char * ns = "test.test1";
+
+ conn.dropCollection(ns);
+
+ // clean up old data from any previous tests
+ conn.remove( ns, BSONObj() );
+ assert( conn.findOne( ns , BSONObj() ).isEmpty() );
+
+ // test insert
+ conn.insert( ns ,BSON( "name" << "eliot" << "num" << 1 ) );
+ assert( ! conn.findOne( ns , BSONObj() ).isEmpty() );
+
+ // test remove
+ conn.remove( ns, BSONObj() );
+ assert( conn.findOne( ns , BSONObj() ).isEmpty() );
+
+
+ // insert, findOne testing
+ conn.insert( ns , BSON( "name" << "eliot" << "num" << 1 ) );
+ {
+ BSONObj res = conn.findOne( ns , BSONObj() );
+ assert( strstr( res.getStringField( "name" ) , "eliot" ) );
+ assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
+ assert( 1 == res.getIntField( "num" ) );
+ }
+
+
+ // cursor
+ conn.insert( ns ,BSON( "name" << "sara" << "num" << 2 ) );
+ {
+ auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() );
+ int count = 0;
+ while ( cursor->more() ) {
+ count++;
+ BSONObj obj = cursor->next();
+ }
+ assert( count == 2 );
+ }
+
+ {
+ auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 1 ) );
+ int count = 0;
+ while ( cursor->more() ) {
+ count++;
+ BSONObj obj = cursor->next();
+ }
+ assert( count == 1 );
+ }
+
+ {
+ auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 3 ) );
+ int count = 0;
+ while ( cursor->more() ) {
+ count++;
+ BSONObj obj = cursor->next();
+ }
+ assert( count == 0 );
+ }
+
+ // update
+ {
+ BSONObj res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
+ assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
+
+ BSONObj after = BSONObjBuilder().appendElements( res ).append( "name2" , "h" ).obj();
+
+ conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after );
+ res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
+ assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) );
+ assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() );
+
+ conn.update( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() , after );
+ res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() );
+ assert( strstr( res.getStringField( "name" ) , "eliot" ) );
+ assert( strstr( res.getStringField( "name2" ) , "h" ) );
+ assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() );
+
+ // upsert
+ conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after , 1 );
+ assert( ! conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ).isEmpty() );
+
+ }
+
+ { // ensure index
+ assert( conn.ensureIndex( ns , BSON( "name" << 1 ) ) );
+ assert( ! conn.ensureIndex( ns , BSON( "name" << 1 ) ) );
+ }
+
+ { // hint related tests
+ assert( conn.findOne(ns, "{}")["name"].str() == "sara" );
+
+ assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" );
+ assert( conn.getLastError() == "" );
+
+ // nonexistent index test
+ assert( conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}")).hasElement("$err") );
+ assert( conn.getLastError() == "bad hint" );
+ conn.resetError();
+ assert( conn.getLastError() == "" );
+
+ //existing index
+ assert( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") );
+
+ // run validate
+ assert( conn.validate( ns ) );
+ }
+
+ { // timestamp test
+
+ const char * tsns = "test.tstest1";
+ conn.dropCollection( tsns );
+
+ {
+ mongo::BSONObjBuilder b;
+ b.appendTimestamp( "ts" );
+ conn.insert( tsns , b.obj() );
+ }
+
+ mongo::BSONObj out = conn.findOne( tsns , mongo::BSONObj() );
+ Date_t oldTime = out["ts"].timestampTime();
+ unsigned int oldInc = out["ts"].timestampInc();
+
+ {
+ mongo::BSONObjBuilder b1;
+ b1.append( out["_id"] );
+
+ mongo::BSONObjBuilder b2;
+ b2.append( out["_id"] );
+ b2.appendTimestamp( "ts" );
+
+ conn.update( tsns , b1.obj() , b2.obj() );
+ }
+
+ BSONObj found = conn.findOne( tsns , mongo::BSONObj() );
+ assert( ( oldTime < found["ts"].timestampTime() ) ||
+ ( oldInc + 1 == found["ts"].timestampInc() ) );
+
+ }
+
+ { // check that killcursors doesn't affect last error
+ assert( conn.getLastError().empty() );
+
+ BufBuilder b;
+ b.append( (int)0 ); // reserved
+ b.append( (int)-1 ); // invalid # of cursors triggers exception
+ b.append( (int)-1 ); // bogus cursor id
+
+ Message m;
+ m.setData( dbKillCursors, b.buf(), b.len() );
+
+ // say() is protected in DBClientConnection, so get superclass
+ static_cast< DBConnector* >( &conn )->say( m );
+
+ assert( conn.getLastError().empty() );
+ }
+
+ {
+ list<string> l = conn.getDatabaseNames();
+ for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){
+ cout << "db name : " << *i << endl;
+ }
+
+ l = conn.getCollectionNames( "test" );
+ for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){
+ cout << "coll name : " << *i << endl;
+ }
+ }
+
+ cout << "client test finished!" << endl;
+}
diff --git a/client/examples/first.cpp b/client/examples/first.cpp
new file mode 100644
index 0000000..f3b654f
--- /dev/null
+++ b/client/examples/first.cpp
@@ -0,0 +1,85 @@
+// first.cpp
+
+/* Copyright 2009 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * this is a good first example of how to use mongo from c++
+ */
+
+#include <iostream>
+
+#include "client/dbclient.h"
+
+using namespace std;
+
+void insert( mongo::DBClientConnection & conn , const char * name , int num ) {
+ mongo::BSONObjBuilder obj;
+ obj.append( "name" , name );
+ obj.append( "num" , num );
+ conn.insert( "test.people" , obj.obj() );
+}
+
+int main( int argc, const char **argv ) {
+
+ const char *port = "27017";
+ if ( argc != 1 ) {
+ if ( argc != 3 )
+ throw -12;
+ port = argv[ 2 ];
+ }
+
+ mongo::DBClientConnection conn;
+ string errmsg;
+ if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
+ cout << "couldn't connect : " << errmsg << endl;
+ throw -11;
+ }
+
+ { // clean up old data from any previous tests
+ mongo::BSONObjBuilder query;
+ conn.remove( "test.people" , query.obj() );
+ }
+
+ insert( conn , "eliot" , 15 );
+ insert( conn , "sara" , 23 );
+
+ {
+ mongo::BSONObjBuilder query;
+ auto_ptr<mongo::DBClientCursor> cursor = conn.query( "test.people" , query.obj() );
+ cout << "using cursor" << endl;
+ while ( cursor->more() ) {
+ mongo::BSONObj obj = cursor->next();
+ cout << "\t" << obj.jsonString() << endl;
+ }
+
+ }
+
+ {
+ mongo::BSONObjBuilder query;
+ query.append( "name" , "eliot" );
+ mongo::BSONObj res = conn.findOne( "test.people" , query.obj() );
+ cout << res.isEmpty() << "\t" << res.jsonString() << endl;
+ }
+
+ {
+ mongo::BSONObjBuilder query;
+ query.append( "name" , "asd" );
+ mongo::BSONObj res = conn.findOne( "test.people" , query.obj() );
+ cout << res.isEmpty() << "\t" << res.jsonString() << endl;
+ }
+
+
+}
diff --git a/client/examples/second.cpp b/client/examples/second.cpp
new file mode 100644
index 0000000..68eafaa
--- /dev/null
+++ b/client/examples/second.cpp
@@ -0,0 +1,56 @@
+// second.cpp
+
+/* Copyright 2009 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+
+#include "client/dbclient.h"
+
+using namespace std;
+using namespace mongo;
+
+int main( int argc, const char **argv ) {
+
+ const char *port = "27017";
+ if ( argc != 1 ) {
+ if ( argc != 3 )
+ throw -12;
+ port = argv[ 2 ];
+ }
+
+ DBClientConnection conn;
+ string errmsg;
+ if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
+ cout << "couldn't connect : " << errmsg << endl;
+ throw -11;
+ }
+
+ const char * ns = "test.second";
+
+ conn.remove( ns , BSONObj() );
+
+ conn.insert( ns , BSON( "name" << "eliot" << "num" << 17 ) );
+ conn.insert( ns , BSON( "name" << "sara" << "num" << 24 ) );
+
+ auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() );
+ cout << "using cursor" << endl;
+ while ( cursor->more() ) {
+ BSONObj obj = cursor->next();
+ cout << "\t" << obj.jsonString() << endl;
+ }
+
+ conn.ensureIndex( ns , BSON( "name" << 1 << "num" << -1 ) );
+}
diff --git a/client/examples/tail.cpp b/client/examples/tail.cpp
new file mode 100644
index 0000000..e844b32
--- /dev/null
+++ b/client/examples/tail.cpp
@@ -0,0 +1,55 @@
+// tail.cpp
+
+/* Copyright 2009 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* example of using a tailable cursor */
+
+#include "../../client/dbclient.h"
+#include "../../util/goodies.h"
+
+using namespace mongo;
+
+void foo() { }
+
+/* "tail" the specified namespace, outputting elements as they are added.
+ _id values must be inserted in increasing order for this to work. (Some other
+ field could also be used.)
+
+ Note: one could use a capped collection and $natural order to do something
+ similar, using sort({$natural:1}), and then not need to worry about
+ _id's being in order.
+*/
+void tail(DBClientBase& conn, const char *ns) {
+ conn.ensureIndex(ns, fromjson("{_id:1}"));
+ BSONElement lastId;
+ Query query = Query().sort("_id");
+ while( 1 ) {
+ auto_ptr<DBClientCursor> c = conn.query(ns, query, 0, 0, 0, Option_CursorTailable);
+ while( 1 ) {
+ if( !c->more() ) {
+ if( c->isDead() ) {
+ // we need to requery
+ break;
+ }
+ sleepsecs(1);
+ }
+ BSONObj o = c->next();
+ lastId = o["_id"];
+ cout << o.toString() << endl;
+ }
+ query = QUERY( "_id" << GT << lastId ).sort("_id");
+ }
+}
diff --git a/client/examples/tutorial.cpp b/client/examples/tutorial.cpp
new file mode 100644
index 0000000..28e1b27
--- /dev/null
+++ b/client/examples/tutorial.cpp
@@ -0,0 +1,67 @@
+//tutorial.cpp
+
+/* Copyright 2009 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+#include "../../client/dbclient.h"
+
+// g++ tutorial.cpp -lmongoclient -lboost_thread -lboost_filesystem -o tutorial
+
+using namespace mongo;
+
+void printIfAge(DBClientConnection& c, int age) {
+ auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") );
+ while( cursor->more() ) {
+ BSONObj p = cursor->next();
+ cout << p.getStringField("name") << endl;
+ }
+}
+
+void run() {
+ DBClientConnection c;
+ c.connect("localhost"); //"192.168.58.1");
+ cout << "connected ok" << endl;
+ BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
+ c.insert("tutorial.persons", p);
+ p = BSON( "name" << "Jane" << "age" << 40 );
+ c.insert("tutorial.persons", p);
+ p = BSON( "name" << "Abe" << "age" << 33 );
+ c.insert("tutorial.persons", p);
+ p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" );
+ c.insert("tutorial.persons", p);
+
+ c.ensureIndex("tutorial.persons", fromjson("{age:1}"));
+
+ cout << "count:" << c.count("tutorial.persons") << endl;
+
+ auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());
+ while( cursor->more() ) {
+ cout << cursor->next().toString() << endl;
+ }
+
+ cout << "\nprintifage:\n";
+ printIfAge(c, 33);
+}
+
+int main() {
+ try {
+ run();
+ }
+ catch( DBException &e ) {
+ cout << "caught " << e.what() << endl;
+ }
+ return 0;
+}
diff --git a/client/examples/whereExample.cpp b/client/examples/whereExample.cpp
new file mode 100644
index 0000000..a26d921
--- /dev/null
+++ b/client/examples/whereExample.cpp
@@ -0,0 +1,68 @@
+// whereExample.cpp
+
+/* Copyright 2009 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+
+#include "client/dbclient.h"
+
+using namespace std;
+using namespace mongo;
+
+int main( int argc, const char **argv ) {
+
+ const char *port = "27017";
+ if ( argc != 1 ) {
+ if ( argc != 3 )
+ throw -12;
+ port = argv[ 2 ];
+ }
+
+ DBClientConnection conn;
+ string errmsg;
+ if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
+ cout << "couldn't connect : " << errmsg << endl;
+ throw -11;
+ }
+
+ const char * ns = "test.where";
+
+ conn.remove( ns , BSONObj() );
+
+ conn.insert( ns , BSON( "name" << "eliot" << "num" << 17 ) );
+ conn.insert( ns , BSON( "name" << "sara" << "num" << 24 ) );
+
+ auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() );
+
+ while ( cursor->more() ) {
+ BSONObj obj = cursor->next();
+ cout << "\t" << obj.jsonString() << endl;
+ }
+
+ cout << "now using $where" << endl;
+
+ Query q = Query("{}").where("this.name == name" , BSON( "name" << "sara" ));
+
+ cursor = conn.query( ns , q );
+
+ int num = 0;
+ while ( cursor->more() ) {
+ BSONObj obj = cursor->next();
+ cout << "\t" << obj.jsonString() << endl;
+ num++;
+ }
+ assert( num == 1 );
+}