MONGO_EXPORT int mongo_remove( mongo *conn, const char *ns, const bson *cond ) { char *data; mongo_message *mm; /* Make sure that the BSON is valid UTF-8. * TODO: decide whether to check cond as well. * */ if( mongo_bson_valid( conn, ( bson * )cond, 0 ) != MONGO_OK ) { return MONGO_ERROR; } mm = mongo_message_create( 16 /* header */ + 4 /* ZERO */ + strlen( ns ) + 1 + 4 /* ZERO */ + bson_size( cond ) , 0 , 0 , MONGO_OP_DELETE ); data = &mm->data; data = mongo_data_append32( data, &ZERO ); data = mongo_data_append( data, ns, strlen( ns ) + 1 ); data = mongo_data_append32( data, &ZERO ); data = mongo_data_append( data, cond->data, bson_size( cond ) ); return mongo_message_send( conn, mm ); }
MONGO_EXPORT int mongo_insert_batch( mongo *conn, const char *ns, const bson **bsons, int count ) { mongo_message *mm; int i; char *data; int size = 16 + 4 + strlen( ns ) + 1; for( i=0; i<count; i++ ) { size += bson_size( bsons[i] ); if( mongo_bson_valid( conn, bsons[i], 1 ) != MONGO_OK ) return MONGO_ERROR; } mm = mongo_message_create( size , 0 , 0 , MONGO_OP_INSERT ); data = &mm->data; data = mongo_data_append32( data, &ZERO ); data = mongo_data_append( data, ns, strlen( ns ) + 1 ); for( i=0; i<count; i++ ) { data = mongo_data_append( data, bsons[i]->data, bson_size( bsons[i] ) ); } return mongo_message_send( conn, mm ); }
int mongo_update( mongo *conn, const char *ns, const bson *cond, const bson *op, int flags ) { char *data; mongo_message *mm; /* Make sure that the op BSON is valid UTF-8. * TODO: decide whether to check cond as well. * */ if( mongo_bson_valid( conn, ( bson * )op, 0 ) != MONGO_OK ) { return MONGO_ERROR; } mm = mongo_message_create( 16 /* header */ + 4 /* ZERO */ + strlen( ns ) + 1 + 4 /* flags */ + bson_size( cond ) + bson_size( op ) , 0 , 0 , MONGO_OP_UPDATE ); data = &mm->data; data = mongo_data_append32( data, &ZERO ); data = mongo_data_append( data, ns, strlen( ns ) + 1 ); data = mongo_data_append32( data, &flags ); data = mongo_data_append( data, cond->data, bson_size( cond ) ); data = mongo_data_append( data, op->data, bson_size( op ) ); return mongo_message_send( conn, mm ); }
MONGO_EXPORT int mongo_insert( mongo *conn , const char *ns , const bson *bson ) { char *data; mongo_message *mm; /* Make sure that BSON is valid for insert. */ if( mongo_bson_valid( conn, bson, 1 ) != MONGO_OK ) { return MONGO_ERROR; } mm = mongo_message_create( 16 /* header */ + 4 /* ZERO */ + strlen( ns ) + 1 + bson_size( bson ) , 0, 0, MONGO_OP_INSERT ); data = &mm->data; data = mongo_data_append32( data, &ZERO ); data = mongo_data_append( data, ns, strlen( ns ) + 1 ); data = mongo_data_append( data, bson->data, bson_size( bson ) ); return mongo_message_send( conn, mm ); }