Files
mongo/client/examples/first.cpp

86 lines
2.4 KiB
C++
Raw Normal View History

2009-01-12 15:27:46 -05:00
// 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.
*/
2009-01-12 15:27:46 -05:00
/**
* this is a good first example of how to use mongo from c++
*/
#include <iostream>
2009-01-29 18:38:35 -05:00
#include "client/dbclient.h"
2009-01-12 15:27:46 -05:00
2009-10-17 21:48:16 -04:00
using namespace std;
void insert( mongo::DBClientConnection & conn , const char * name , int num ) {
mongo::BSONObjBuilder obj;
2009-01-12 15:27:46 -05:00
obj.append( "name" , name );
obj.append( "num" , num );
2009-02-09 13:04:32 -05:00
conn.insert( "test.people" , obj.obj() );
2009-01-12 15:27:46 -05:00
}
int main( int argc, const char **argv ) {
2009-01-12 15:27:46 -05:00
const char *port = "27017";
if ( argc != 1 ) {
if ( argc != 3 )
throw -12;
port = argv[ 2 ];
}
mongo::DBClientConnection conn;
2009-01-12 15:27:46 -05:00
string errmsg;
if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
2009-01-12 15:27:46 -05:00
cout << "couldn't connect : " << errmsg << endl;
throw -11;
}
{ // clean up old data from any previous tests
mongo::BSONObjBuilder query;
2009-02-09 13:04:32 -05:00
conn.remove( "test.people" , query.obj() );
2009-01-12 15:27:46 -05:00
}
2009-01-14 17:17:24 -05:00
2009-01-12 15:27:46 -05:00
insert( conn , "eliot" , 15 );
insert( conn , "sara" , 23 );
2009-01-12 15:27:46 -05:00
{
mongo::BSONObjBuilder query;
2009-02-09 13:04:32 -05:00
auto_ptr<mongo::DBClientCursor> cursor = conn.query( "test.people" , query.obj() );
2009-01-12 15:27:46 -05:00
cout << "using cursor" << endl;
2009-01-14 17:17:24 -05:00
while ( cursor->more() ) {
mongo::BSONObj obj = cursor->next();
2009-01-12 15:27:46 -05:00
cout << "\t" << obj.jsonString() << endl;
}
2009-01-14 17:17:24 -05:00
2009-01-12 15:27:46 -05:00
}
2009-01-12 15:27:46 -05:00
{
mongo::BSONObjBuilder query;
2009-01-12 15:27:46 -05:00
query.append( "name" , "eliot" );
2009-02-09 13:04:32 -05:00
mongo::BSONObj res = conn.findOne( "test.people" , query.obj() );
2009-01-12 15:27:46 -05:00
cout << res.isEmpty() << "\t" << res.jsonString() << endl;
}
2009-01-12 15:27:46 -05:00
{
mongo::BSONObjBuilder query;
2009-01-12 15:27:46 -05:00
query.append( "name" , "asd" );
2009-02-09 13:04:32 -05:00
mongo::BSONObj res = conn.findOne( "test.people" , query.obj() );
2009-01-12 15:27:46 -05:00
cout << res.isEmpty() << "\t" << res.jsonString() << endl;
}
2009-01-14 17:17:24 -05:00
2009-01-12 15:27:46 -05:00
}