Writing out from aggregates in a loop does not work v5.1.7

Jase Kurasz shared this problem 4 months ago
Solved

in the below example lets say I have a "mainDB" that consits of two documents with a field1 containing a string guid. If i try to run this code below to put those docs into 2 new collections it does not work.

var dict = {}

dict['collection1'] = ["zzPiUErfOJIRCC5bOiCjEmbr3IwgIzVK", "zviZyfPAyNib6W0yUCxLbN8gjjp0HEUf"]

dict['collection2'] = ["zzPiUErfOJIRCC5bOiCjEmbr3IwgIzVK", "zviZyfPAyNib6W0yUCxLbN8gjjp0HEUf"]


Object.keys(dict).forEach(function(key) {

// create the collection

db.createCollection(key);

// clear it out

db.getCollection(key).remove({});

// write out results of aggregate to the collection

db.getCollection('mainDB').aggregate([{$match: {field1: {$in: dict[key]}}}, {$out: key}]);

}

In fact I noticed that even trying to do just the following this will work BUT if i have any code after this then it will not write out to that collection

// create the collection

db.createCollection('collection3');

// clear it out

db.getCollection('collection3').remove({});

// write out results of aggregate to the collection

db.getCollection('mainDB').aggregate([{$match: {field1: {$in: ["zzPiUErfOJIRCC5bOiCjEmbr3IwgIzVK", "zviZyfPAyNib6W0yUCxLbN8gjjp0HEUf"]}}}, {$out: 'collection3'}]);

Comments (8)

photo
1

I don't quite understand your description, can you give the complete replay steps by following the list below?

Thanks for opening your issue here! If you're reporting a bug, please make sure you include steps to reproduce it.

  • NoSQLBooster Version
  • Operating System (Windows/Mac/Ubuntu):
  • MongoDB Version
  • Expected Behavior
  • Actual Behavior
  • How to Reproduce (Please attach sample JSON data)

photo
1
  • NoSQLBooster Version: 5.1.7
  • Operating System (Windows/Mac/Ubuntu): Mac Version 10.14.4
  • MongoDB Version: 3.4.20
  • Expected Behavior: add { _id:1, item: "a01"} and { _id:2, item: "b02"} records to collection1 and collection2
  • Actual Behavior: nothing is added to either collection
  • How to Reproduce: see following code that i modified from one of your samples
  • use NoSQLBoosterSamples
    
    print(`prepare test data`);
    db.bugRepro.drop();
    db.bugRepro.insert([
        { _id:1, item: "a01"},
        { _id:2, item: "b02"},
        { _id:3, item: "c03"},
        { _id:4, item: "d04"},
        { _id:5, item: "e05"},
     ]);
    
    // nothing is added to collection 1 or 2
    var collectionNames = ["testCollection1", "testCollection2"];
    for(var i = 0; i < collectionNames.length; i++) {
        db.createCollection(collectionNames[i]);
        db.getCollection(collectionNames[i]).aggregate([{$match: {item: {$in: ["a01", "b02"]}}}, {$out: collectionNames[i]}]);
    }

photo
1

sorry the above code is wrong, this is what i meant to put

print(`prepare test data`);
db.bugRepro.drop();
db.bugRepro.insert([
    { _id:1, item: "a01"},
    { _id:2, item: "b02"},
    { _id:3, item: "c03"},
    { _id:4, item: "d04"},
    { _id:5, item: "e05"},
 ]);

// nothing is added to collection 1 or 2
var collectionNames = ["testCollection1", "testCollection2"];
for(var i = 0; i < collectionNames.length; i++) {
    db.createCollection(collectionNames[i]);
    db.bugRepro.aggregate([{$match: {item: {$in: ["a01", "b02"]}}}, {$out: collectionNames[i]}]);
}

// note that if this print line is not here then collection 2 will have the documents in it but with this print neither do
print("finished");

photo
1

Thank you for your feedback.

We have worked out a new test build to resolve this issue, please download and give it a try.

https://nosqlbooster.com/s3/download/releasesv5/nosqlbooster4mongo-5.1.8-beta.2.exe

photo
1

I'm running on a macbook, do you have a .dmg?

photo
1

Looks like its fixed, thank you!

photo
1

Resolved in 5.1.8