{"id":750,"date":"2018-03-24T16:34:06","date_gmt":"2018-03-24T16:34:06","guid":{"rendered":"http:\/\/www.andreagirardi.it\/blog\/?p=750"},"modified":"2018-03-24T17:16:29","modified_gmt":"2018-03-24T17:16:29","slug":"find-on-different-collection-to-create-a-document-using-mongoose","status":"publish","type":"post","link":"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/","title":{"rendered":"Find on different collections to create a document using mongoose"},"content":{"rendered":"<p>I need to query multiple collections to prepare a MongoDB document and then save it using Mongoose and NodeJS. The solution is to use <strong>async.parallel<\/strong><br \/>\nI have two source collections, <em>Robot<\/em>, <em>Target<\/em> and the destination collection <em>Activity<\/em>.<\/p>\n<p>For first, <strong>async<\/strong> must be added:<\/p>\n<pre lang=\"javascript\">var async = require('async');<\/pre>\n<p>then:<\/p>\n<pre lang=\"javascript\">\r\nasync.parallel({\r\n\r\n    robotFind: function(cb) { Robot.find({ \"_id\": jsonContent.robotId }).exec(cb); },\r\n    targetFind: function(cb) { Target.find({ \"_id\": jsonContent.targetId }).exec(cb); }\r\n\r\n}, function(err, result) {\r\n\r\n    activity.robot = result.robotFind[0];\r\n    activity.action = result.actionFind[0];\r\n    activity.target = result.targetFind[0];\r\n\r\n    activity.execution_date = jsonContent.execution_date;\r\n    activity.alert = jsonContent.alert;\r\n    activity.result = executionResult;\r\n\r\n    activity.description = jsonContent.description;\r\n\r\n    activity.save(function(err) {\r\n        if (err) {\r\n            console.log('[postActivity] ' + err)\r\n            res.status(500).json({ error: err.message })\r\n        } else {\r\n            console.log('[postActivity] Saved!')\r\n            res.status(200).json({ message: activity })\r\n        }\r\n    })\r\n}\r\n<\/pre>\n<p>So, first part queries the MongoDB and fill the object result. Second part consumes result object, create the new document and save it.<\/p>\n<p>PS: if you like it please, click on the banner :)<br \/>\n[AdSense-B]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-750\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-750\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-750\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-750\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>I need to query multiple collections to prepare a MongoDB document and then save it using Mongoose and NodeJS. The solution is to use async.parallel I have two source collections, Robot, Target and the destination collection Activity. For first, async must be added: var async = require(&#8216;async&#8217;); then: async.parallel({ robotFind: function(cb) { Robot.find({ &#8220;_id&#8221;: jsonContent.robotId [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-750\" class=\"share-linkedin sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span>LinkedIn<\/span><\/a><\/li><li class=\"share-reddit\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\"><span>Reddit<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-750\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-750\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-skype\"><a rel=\"nofollow\" data-shared=\"sharing-skype-750\" class=\"share-skype sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=skype\" target=\"_blank\" title=\"Share on Skype\"><span>Skype<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon\" href=\"https:\/\/www.andreagirardi.it\/blog\/find-on-different-collection-to-create-a-document-using-mongoose\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span>Pocket<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[14],"tags":[32,63,64],"_links":{"self":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/750"}],"collection":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/comments?post=750"}],"version-history":[{"count":5,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/750\/revisions"}],"predecessor-version":[{"id":760,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/posts\/750\/revisions\/760"}],"wp:attachment":[{"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/media?parent=750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/categories?post=750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andreagirardi.it\/blog\/wp-json\/wp\/v2\/tags?post=750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}