Built-in tools for partitioning within a document?

18 views
Skip to first unread message

dpapathanasiou

unread,
Jun 17, 2017, 2:04:27 PM6/17/17
to mongodb-user
I'm using update() with both the $set and $push operators simultaneously,
in conjunction with the upsert option set to true.

The idea is that for any given, document, $set will update the
entire doc in full (or just insert it if it's new) and, in the same
atomic operation, append the document to a vector for tracking
history.

Using the java driver, it looks like this:

BasicDBObject update = new BasicDBObject()
    .append("$set", currentDoc)
    .append("$push", new BasicDBObject().append("history", currentDoc));

collection.update(query, update, true, false);

Since "history" will grow over time, I'd like a way of partitioning
by date (currentDoc includes a $currentDate field), so that I can
purge anything older than n days in that vector, while keeping the
rest of the document as-is.

TTL indexes would be ideal, if I were maintaining the history as
separate documents in their own right.

It seems, though, that I cannot do it that way b/c of the lack of
transaction support, because the actions involve multiple documents:

I.e., doing an insert with history requires finding the latest
version of the document, moving or reinserting it with a different
_id, and finally inserting the new version.

dpapathanasiou

unread,
Jun 22, 2017, 7:57:45 PM6/22/17
to mongodb-user
Just to close the loop on this, I wound up using $pull within an
update() statement, which does exactly what I need.

So for posterity and/or if anyone else has this question, here's a
working sketch from the mongo shell:

> db.accounts.update({_id: "id"}: {$pull: {history: {inserted: {$lte new ISODate("desired cutoff")}}}})

Reply all
Reply to author
Forward
0 new messages