Skip to content

Instantly share code, notes, and snippets.

@leolara
Last active December 23, 2015 13:09
Show Gist options
  • Save leolara/6640602 to your computer and use it in GitHub Desktop.
Save leolara/6640602 to your computer and use it in GitHub Desktop.
Add reference to listings
var setReferenceId = {
getNext: function () {
var seq = db.sequences.findAndModify( { query: { _id: 'listings' } , update : { $inc : { sequence: 1} }, new: true, upsert: true });
return seq.sequence;
},
getCursor: function () {
return db.listings.find( { reference_id: { $exists: false } }, {});
},
setReference: function (id, ref) {
return db.listings.update({ _id: id}, { $set: { reference_id: ref }});
},
run: function () {
var self = this;
var count = 0;
this.getCursor().forEach( function (doc) { self.setReference(doc._id, self.getNext()); print(++count); });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment