Skip to content

Instantly share code, notes, and snippets.

@neunhoef
Created October 20, 2016 08:44
Show Gist options
  • Save neunhoef/079a461899fda51cbfdf704759653809 to your computer and use it in GitHub Desktop.
Save neunhoef/079a461899fda51cbfdf704759653809 to your computer and use it in GitHub Desktop.
db._drop("fromCollection");
db._drop("toCollection");
db._drop("edgeCollection");
var fromCollection = db._create("fromCollection", {numberOfShards:2})
var toCollection = db._create("toCollection", {numberOfShards:2})
fromCollection.ensureIndex({type:"hash",unique:false,fields:["fromAttributeValue"]});
toCollection.ensureIndex({type:"hash",unique:false,fields:["toAttributeValue"]});
fromCollection.ensureIndex({type:"skiplist",unique:false,fields:["timeStamp"]});
toCollection.ensureIndex({type:"skiplist",unique:false,fields:["timeStamp"]});
var edgeCollection = db._createEdgeCollection("edgeCollection", {numberOfShards:2});
function update(lastImport) {
db._explain(`FOR fromItem IN fromCollection
FILTER fromItem.timeStamp > @lastRun
FOR toItem IN toCollection
FILTER fromItem.fromAttributeValue == toItem.toAttributeValue
INSERT { _from: fromItem._id, _to: toItem._id, otherAttributes: {}} INTO edgeCollection`,
{lastRun:lastImport});
db._query(`FOR fromItem IN fromCollection
FILTER fromItem.timeStamp > @lastRun
FOR toItem IN toCollection
FILTER fromItem.fromAttributeValue == toItem.toAttributeValue
INSERT { _from: fromItem._id, _to: toItem._id, otherAttributes: {}} INTO edgeCollection`,
{lastRun:lastImport}).toArray();
db._explain(`FOR toItem IN toCollection
FILTER toItem.timeStamp > @lastRun
FOR fromItem IN fromCollection
FILTER fromItem.fromAttributeValue == toItem.toAttributeValue
FILTER fromItem.timeStamp <= @lastRun
INSERT { _from: fromItem._id, _to: toItem._id, otherAttributes: {}} INTO edgeCollection`,
{lastRun:lastImport});
db._query(`FOR toItem IN toCollection
FILTER toItem.timeStamp > @lastRun
FOR fromItem IN fromCollection
FILTER fromItem.fromAttributeValue == toItem.toAttributeValue
FILTER fromItem.timeStamp <= @lastRun
INSERT { _from: fromItem._id, _to: toItem._id, otherAttributes: {}} INTO edgeCollection`,
{lastRun:lastImport});
}
fromCollection.insert({fromAttributeValue: 1, timeStamp:1});
fromCollection.insert({fromAttributeValue: 1, timeStamp:1});
fromCollection.insert({fromAttributeValue: 2, timeStamp:1});
toCollection.insert({toAttributeValue: 1, timeStamp:1});
toCollection.insert({toAttributeValue: 2, timeStamp:1});
toCollection.insert({toAttributeValue: 3, timeStamp:1});
update(0);
fromCollection.insert({fromAttributeValue: 4, timeStamp:2});
fromCollection.insert({fromAttributeValue: 4, timeStamp:2});
fromCollection.insert({fromAttributeValue: 5, timeStamp:2});
toCollection.insert({toAttributeValue: 4, timeStamp:2});
toCollection.insert({toAttributeValue: 5, timeStamp:2});
toCollection.insert({toAttributeValue: 6, timeStamp:2});
update(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment