Bug #96686 Document not added in Collection if in while loop
Submitted: 28 Aug 2019 19:29 Modified: 30 Aug 2019 17:14
Reporter: Ralf Adams Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Document Store: MySQL Shell Severity:S2 (Serious)
Version:8.0.17 OS:Windows
Assigned to: MySQL Verification Team CPU Architecture:Any
Tags: Add(), Collection, javascript, while

[28 Aug 2019 19:29] Ralf Adams
Description:
I was using the mysqlsh in JavaScript mode. I tried to add foreach row in a table a document to a collection. In order to do so, I placed the coll.add() statement inside a while loop. But, a coll.find() returned an empty set. 

I created a short example which shows the behaviour: 
- There is a coll.add() in line 8 and this document is listed as you can see in the comment below the source code.
- There is a call.add() in line 11 inside a while loop. The loop itself works as you can see in the comment below: there is the output "123". But the documents are not added. 

If I was running the statement 3 times without a loop (manualy) 3 documents were inserted.

How to repeat:
var mysqlx = require('mysqlx');
var mySession = mysqlx.getSession({host: 'localhost', port: 33060,
                                   user: 'root', password: 'Bochum2019!'});
var myDB = mySession.getSchema('test');
myDB.dropCollection('mycoll');
var coll = myDB.createCollection('mycoll');

coll.add({a:0});              // works
var n=0;
while (n < 3) {
	coll.add({a:2});          // works not
	n=n+1;
	print(n);
}

coll.find();
mySession.close();

/*
 MySQL  JS > \source test.js
Query OK, 1 item affected (0.1458 sec)
123{
    "a": 0,
    "_id": "00005d6523200000000000000015"
}
1 document in set (0.0005 sec)
*/
[30 Aug 2019 11:43] MySQL Verification Team
Hi,

You are missing .execute();

e.g. 

[arhimed@localhost msb_8_0_15]$ cat xx.js
var mysqlx = require('mysqlx');
var mySession = mysqlx.getSession({host: 'localhost', port: 18015,
                                   user: 'msandbox', password: 'msandbox'});
var myDB = mySession.getSchema('test');
myDB.dropCollection('mycoll');
var coll = myDB.createCollection('mycoll');

coll.add({a:0}).execute();
var n=0;
while (n < 9) {
        coll.add({a:2}).execute();
        n=n+1;
        print(n); print("\n");
}

coll.find();
mySession.close();

[arhimed@localhost msb_8_0_15]$ ./mysqlsh

 MySQL  127.0.0.1:18015+ ssl  JS > \source xx.js
Query OK, 1 item affected (0.0017 sec)
1
2
3
4
5
6
7
8
9
[
    {
        "a": 0,
        "_id": "00005d69067b0000000000000014"
    },
    {
        "a": 2,
        "_id": "00005d69067b0000000000000015"
    },
    {
        "a": 2,
        "_id": "00005d69067b0000000000000016"
    },
    {
        "a": 2,
        "_id": "00005d69067b0000000000000017"
    },
    {
        "a": 2,
        "_id": "00005d69067b0000000000000018"
    },
    {
        "a": 2,
        "_id": "00005d69067b0000000000000019"
    },
    {
        "a": 2,
        "_id": "00005d69067b000000000000001a"
    },
    {
        "a": 2,
        "_id": "00005d69067b000000000000001b"
    },
    {
        "a": 2,
        "_id": "00005d69067b000000000000001c"
    },
    {
        "a": 2,
        "_id": "00005d69067b000000000000001d"
    }
]
10 documents in set (0.0003 sec)

 MySQL  127.0.0.1:18015+ ssl  JS >
[30 Aug 2019 11:44] MySQL Verification Team
examples: https://dev.mysql.com/doc/x-devapi-userguide/en/collection-crud-function-overview.html
[30 Aug 2019 17:14] Ralf Adams
Shame on me. Your're right, my mistake. Thanks.