{"action":"created","issue":{"url":"https://api.github.com/repos/balderdashy/sails-mongo/issues/371","labels_url":"https://api.github.com/repos/balderdashy/sails-mongo/issues/371/labels{/name}","comments_url":"https://api.github.com/repos/balderdashy/sails-mongo/issues/371/comments","events_url":"https://api.github.com/repos/balderdashy/sails-mongo/issues/371/events","html_url":"https://github.com/balderdashy/sails-mongo/issues/371","id":121246302,"number":371,"title":"[question] One-to-many performance","user":{"login":"japel","id":2742448,"avatar_url":"https://avatars.githubusercontent.com/u/2742448?v=3","gravatar_id":"","url":"https://api.github.com/users/japel","html_url":"https://github.com/japel","followers_url":"https://api.github.com/users/japel/followers","following_url":"https://api.github.com/users/japel/following{/other_user}","gists_url":"https://api.github.com/users/japel/gists{/gist_id}","starred_url":"https://api.github.com/users/japel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/japel/subscriptions","organizations_url":"https://api.github.com/users/japel/orgs","repos_url":"https://api.github.com/users/japel/repos","events_url":"https://api.github.com/users/japel/events{/privacy}","received_events_url":"https://api.github.com/users/japel/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-12-09T14:02:29Z","updated_at":"2015-12-16T08:01:52Z","closed_at":null,"body":"Consider this scenario:\r\n\r\n```javascript\r\nvar ModelA = {\r\n  attributes: {\r\n    type: {\r\n      type: \"string\",\r\n      enum: [\r\n        \"typeA\",\r\n        \"typeB\"\r\n      ]\r\n    },\r\n    children: {\r\n      collection: \"ModelA\",\r\n      via: \"parent\"\r\n    },\r\n    parent: {\r\n      model: \"ModelA\"\r\n    }\r\n  }\r\n};\r\n```\r\n\r\nSo lets say I have 50k documents of type \"typeA\" and 5k of type \"typeB\".\r\nI've seen a great performance difference between this two operations which practically end in the same result:\r\n\r\nOperation1:\r\n```javascript\r\n    ModelA.find({type: \"typeA\"})\r\n      .exec(function (error, found) {\r\n        var cursor = Q.when();\r\n        for (var i = 0, len = found.length; i < len; i++) {\r\n          (handleOne)(found[i]);\r\n        }\r\n\r\n        cursor.then(function () {\r\n          console.log(\"all done\");\r\n        });\r\n\r\n        function handleOne(one) {\r\n          cursor = cursor.then(function () {\r\n            var d = Q.defer();\r\n            ModelA.findOne({type: \"typeB\", someId: one.someId})\r\n              .exec(function (error, typeb) {\r\n                if (error) {\r\n                  console.log(error);\r\n                }\r\n                else if (!typeb) {\r\n                  console.log(\"typeb not found\");\r\n                }\r\n                else if (typeb) {\r\n                  device.children.add(one.id);\r\n                  device.save(function (error, saved) {\r\n                    if (error) {\r\n                      console.log(error);\r\n                    }\r\n                    else if (saved) {\r\n                      d.resolve();\r\n                    }\r\n                  });\r\n                }\r\n              });\r\n            return d.promise;\r\n          });\r\n          return cursor;\r\n        }\r\n      });\r\n```\r\n\r\nOperation2:\r\n```javascript\r\n    ModelA.find({type: \"typeA\"})\r\n      .exec(function (error, found) {\r\n        var cursor = Q.when();\r\n        for (var i = 0, len = found.length; i < len; i++) {\r\n          (handleOne)(found[i]);\r\n        }\r\n\r\n        cursor.then(function () {\r\n          console.log(\"all done\");\r\n        });\r\n\r\n        function handleOne(one, i) {\r\n          cursor = cursor.then(function () {\r\n            var d = Q.defer();\r\n            ModelA.findOne({type: \"typeB\", someId: one.someId})\r\n              .exec(function (error, typeb) {\r\n                if (error) {\r\n                  console.log(error);\r\n                }\r\n                else if (!typeb) {\r\n                  console.log(\"typeb not found\");\r\n                }\r\n                else if (typeb) {\r\n                  one.parent = typeb.id;\r\n                  one.save(function (error, saved) {\r\n                    if (error) {\r\n                      console.log(error);\r\n                    }\r\n                    else if (saved) {\r\n                      d.resolve();\r\n                    }\r\n                  });\r\n                }\r\n              });\r\n            return d.promise;\r\n          });\r\n          return cursor;\r\n        }\r\n      });\r\n```\r\nOperation2 is 1000 times faster than Operation1\r\nCould someone with deep waterline/sails-mongo undestanding tell me why this is so?"},"comment":{"url":"https://api.github.com/repos/balderdashy/sails-mongo/issues/comments/165026859","html_url":"https://github.com/balderdashy/sails-mongo/issues/371#issuecomment-165026859","issue_url":"https://api.github.com/repos/balderdashy/sails-mongo/issues/371","id":165026859,"user":{"login":"japel","id":2742448,"avatar_url":"https://avatars.githubusercontent.com/u/2742448?v=3","gravatar_id":"","url":"https://api.github.com/users/japel","html_url":"https://github.com/japel","followers_url":"https://api.github.com/users/japel/followers","following_url":"https://api.github.com/users/japel/following{/other_user}","gists_url":"https://api.github.com/users/japel/gists{/gist_id}","starred_url":"https://api.github.com/users/japel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/japel/subscriptions","organizations_url":"https://api.github.com/users/japel/orgs","repos_url":"https://api.github.com/users/japel/repos","events_url":"https://api.github.com/users/japel/events{/privacy}","received_events_url":"https://api.github.com/users/japel/received_events","type":"User","site_admin":false},"created_at":"2015-12-16T08:01:52Z","updated_at":"2015-12-16T08:01:52Z","body":"@devinivy I'll take a look at it and let you know"}}