Skip to content

Instantly share code, notes, and snippets.

@awsiv
Created March 15, 2012 01:00
Show Gist options
  • Save awsiv/2040848 to your computer and use it in GitHub Desktop.
Save awsiv/2040848 to your computer and use it in GitHub Desktop.
Remove data older than a certain time period from an array
//gcc -static -std=c99 -Wall remove_old_data.c -I./src -L. -lbson -lmongoc -o remove_old_data
/* remove_old_data.c */
#include "bson.h"
#include "mongo.h"
int main() {
bson_buffer bb;
bson cond;
bson empty;
time_t oldStamp = 1331772600;
bson_buffer_init(&bb);
bson_buffer *bbPull = bson_append_start_object(&bb, "$pull");
bson_buffer *bbKey = bson_append_start_object(bbPull, "changes");
bson_buffer *bbTimestamp = bson_append_start_object(bbKey, "time");
bson_append_int(bbTimestamp, "$lte", oldStamp);
bson_append_finish_object(bbTimestamp);
bson_append_finish_object(bbKey);
bson_append_finish_object(bbPull);
bson_from_buffer(&cond, &bb);
const char * ns = "test.main";
mongo_connection conn[1];
mongo_connect( conn , NULL );
mongo_update(conn, ns, bson_empty(&empty), &cond, MONGO_UPDATE_MULTI);
mongo_destroy(conn);
bson_destroy(&cond);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment