How do I achieve my desired functionality using Node.js












-2














Here's my requirement



Pass a array of id's to some function which will do the following




  1. Check if a document exists in Mongodb if YES skip to next id if No create a document with specified id


  2. If all id's of array exist in db return a message all id's exist if not return message created missing documents



Tried several ways to do it but nothing was successful as desired.



Problems I ran into, missing documents created but the message all id's exist returned first before creation completed.



Tried using async but dint help. tried using array length and count of found docs this returned undefined.



**I'm using mongo native client and there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once, before the previous step has finished and I understand node is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.










share|improve this question




















  • 3




    When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. See How to create a Minimal, Complete, and Verifiable example.
    – georgeawg
    Nov 20 at 21:36
















-2














Here's my requirement



Pass a array of id's to some function which will do the following




  1. Check if a document exists in Mongodb if YES skip to next id if No create a document with specified id


  2. If all id's of array exist in db return a message all id's exist if not return message created missing documents



Tried several ways to do it but nothing was successful as desired.



Problems I ran into, missing documents created but the message all id's exist returned first before creation completed.



Tried using async but dint help. tried using array length and count of found docs this returned undefined.



**I'm using mongo native client and there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once, before the previous step has finished and I understand node is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.










share|improve this question




















  • 3




    When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. See How to create a Minimal, Complete, and Verifiable example.
    – georgeawg
    Nov 20 at 21:36














-2












-2








-2







Here's my requirement



Pass a array of id's to some function which will do the following




  1. Check if a document exists in Mongodb if YES skip to next id if No create a document with specified id


  2. If all id's of array exist in db return a message all id's exist if not return message created missing documents



Tried several ways to do it but nothing was successful as desired.



Problems I ran into, missing documents created but the message all id's exist returned first before creation completed.



Tried using async but dint help. tried using array length and count of found docs this returned undefined.



**I'm using mongo native client and there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once, before the previous step has finished and I understand node is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.










share|improve this question















Here's my requirement



Pass a array of id's to some function which will do the following




  1. Check if a document exists in Mongodb if YES skip to next id if No create a document with specified id


  2. If all id's of array exist in db return a message all id's exist if not return message created missing documents



Tried several ways to do it but nothing was successful as desired.



Problems I ran into, missing documents created but the message all id's exist returned first before creation completed.



Tried using async but dint help. tried using array length and count of found docs this returned undefined.



**I'm using mongo native client and there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once, before the previous step has finished and I understand node is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.







javascript node.js mongodb mongoose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 21:45

























asked Nov 20 at 20:49









David

106




106








  • 3




    When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. See How to create a Minimal, Complete, and Verifiable example.
    – georgeawg
    Nov 20 at 21:36














  • 3




    When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. See How to create a Minimal, Complete, and Verifiable example.
    – georgeawg
    Nov 20 at 21:36








3




3




When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. See How to create a Minimal, Complete, and Verifiable example.
– georgeawg
Nov 20 at 21:36




When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. See How to create a Minimal, Complete, and Verifiable example.
– georgeawg
Nov 20 at 21:36












2 Answers
2






active

oldest

votes


















0














It's been a while since I've used MongoDB, but there was an upsert flag, if I recall. You could do an upsert with $set:{_id: id}, and that should create blank documents for you with those id's. Make sure to have a backup of the db, in case you forget to put the set, because otherwise it'll update all your old documents as well, and make them blank. There should be very little nodejs needed for this, because mongo has that functionality for you in their query language. As for the All id's exists bit, I'm sure the documentation for upsert contains some useful info:
https://docs.mongodb.com/manual/reference/method/Bulk.find.upsert/



Edit:

I dug deeper. Upsert might only work on single document updates. In this case, you should just foreach over your id's and run individual updates for each.
https://docs.mongodb.com/manual/reference/method/db.collection.update/#upsert-parameter






share|improve this answer





















  • Sorry if I'm not clear with my question, there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once even before the previous step has finished it is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.
    – David
    Nov 20 at 21:27










  • @David I think what you need is the async module. See caolan.github.io/async/docs.html and stackoverflow.com/questions/25705067/…. Alternatively, you can chain promises or callbacks in node to do what you want.
    – Kevin Adistambha
    Nov 21 at 0:29



















0














If you need to output the array of created documents, the easest way will be to use find. But if you need just to a count of the new elements, and they has same structure, you can use a trick with update operator. That will looks like update({ id: { $nin: arrayOfId }}, { $set: newDoc }, { upsert: true }), where newDoc is a structure of the new doc, and arrayOfId is an array of you id (you can work with _id same way). This operation returns a count of new documents.



And if you try to make a request from angular before component created, use the preloader. And, after you get the result - show it to user. Hope this will help you.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401296%2fhow-do-i-achieve-my-desired-functionality-using-node-js%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It's been a while since I've used MongoDB, but there was an upsert flag, if I recall. You could do an upsert with $set:{_id: id}, and that should create blank documents for you with those id's. Make sure to have a backup of the db, in case you forget to put the set, because otherwise it'll update all your old documents as well, and make them blank. There should be very little nodejs needed for this, because mongo has that functionality for you in their query language. As for the All id's exists bit, I'm sure the documentation for upsert contains some useful info:
    https://docs.mongodb.com/manual/reference/method/Bulk.find.upsert/



    Edit:

    I dug deeper. Upsert might only work on single document updates. In this case, you should just foreach over your id's and run individual updates for each.
    https://docs.mongodb.com/manual/reference/method/db.collection.update/#upsert-parameter






    share|improve this answer





















    • Sorry if I'm not clear with my question, there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once even before the previous step has finished it is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.
      – David
      Nov 20 at 21:27










    • @David I think what you need is the async module. See caolan.github.io/async/docs.html and stackoverflow.com/questions/25705067/…. Alternatively, you can chain promises or callbacks in node to do what you want.
      – Kevin Adistambha
      Nov 21 at 0:29
















    0














    It's been a while since I've used MongoDB, but there was an upsert flag, if I recall. You could do an upsert with $set:{_id: id}, and that should create blank documents for you with those id's. Make sure to have a backup of the db, in case you forget to put the set, because otherwise it'll update all your old documents as well, and make them blank. There should be very little nodejs needed for this, because mongo has that functionality for you in their query language. As for the All id's exists bit, I'm sure the documentation for upsert contains some useful info:
    https://docs.mongodb.com/manual/reference/method/Bulk.find.upsert/



    Edit:

    I dug deeper. Upsert might only work on single document updates. In this case, you should just foreach over your id's and run individual updates for each.
    https://docs.mongodb.com/manual/reference/method/db.collection.update/#upsert-parameter






    share|improve this answer





















    • Sorry if I'm not clear with my question, there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once even before the previous step has finished it is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.
      – David
      Nov 20 at 21:27










    • @David I think what you need is the async module. See caolan.github.io/async/docs.html and stackoverflow.com/questions/25705067/…. Alternatively, you can chain promises or callbacks in node to do what you want.
      – Kevin Adistambha
      Nov 21 at 0:29














    0












    0








    0






    It's been a while since I've used MongoDB, but there was an upsert flag, if I recall. You could do an upsert with $set:{_id: id}, and that should create blank documents for you with those id's. Make sure to have a backup of the db, in case you forget to put the set, because otherwise it'll update all your old documents as well, and make them blank. There should be very little nodejs needed for this, because mongo has that functionality for you in their query language. As for the All id's exists bit, I'm sure the documentation for upsert contains some useful info:
    https://docs.mongodb.com/manual/reference/method/Bulk.find.upsert/



    Edit:

    I dug deeper. Upsert might only work on single document updates. In this case, you should just foreach over your id's and run individual updates for each.
    https://docs.mongodb.com/manual/reference/method/db.collection.update/#upsert-parameter






    share|improve this answer












    It's been a while since I've used MongoDB, but there was an upsert flag, if I recall. You could do an upsert with $set:{_id: id}, and that should create blank documents for you with those id's. Make sure to have a backup of the db, in case you forget to put the set, because otherwise it'll update all your old documents as well, and make them blank. There should be very little nodejs needed for this, because mongo has that functionality for you in their query language. As for the All id's exists bit, I'm sure the documentation for upsert contains some useful info:
    https://docs.mongodb.com/manual/reference/method/Bulk.find.upsert/



    Edit:

    I dug deeper. Upsert might only work on single document updates. In this case, you should just foreach over your id's and run individual updates for each.
    https://docs.mongodb.com/manual/reference/method/db.collection.update/#upsert-parameter







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 at 21:17









    djak250

    93




    93












    • Sorry if I'm not clear with my question, there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once even before the previous step has finished it is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.
      – David
      Nov 20 at 21:27










    • @David I think what you need is the async module. See caolan.github.io/async/docs.html and stackoverflow.com/questions/25705067/…. Alternatively, you can chain promises or callbacks in node to do what you want.
      – Kevin Adistambha
      Nov 21 at 0:29


















    • Sorry if I'm not clear with my question, there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once even before the previous step has finished it is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.
      – David
      Nov 20 at 21:27










    • @David I think what you need is the async module. See caolan.github.io/async/docs.html and stackoverflow.com/questions/25705067/…. Alternatively, you can chain promises or callbacks in node to do what you want.
      – Kevin Adistambha
      Nov 21 at 0:29
















    Sorry if I'm not clear with my question, there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once even before the previous step has finished it is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.
    – David
    Nov 20 at 21:27




    Sorry if I'm not clear with my question, there's absolutely no problem with mongo queries in this case. The problem is with Node.js . node executes all at once even before the previous step has finished it is not sequential. since looking for a document and creating it takes time the next steps are executed first and then after all DB process is completed. I'm looking for a solution that will go step by step in a sequence.
    – David
    Nov 20 at 21:27












    @David I think what you need is the async module. See caolan.github.io/async/docs.html and stackoverflow.com/questions/25705067/…. Alternatively, you can chain promises or callbacks in node to do what you want.
    – Kevin Adistambha
    Nov 21 at 0:29




    @David I think what you need is the async module. See caolan.github.io/async/docs.html and stackoverflow.com/questions/25705067/…. Alternatively, you can chain promises or callbacks in node to do what you want.
    – Kevin Adistambha
    Nov 21 at 0:29













    0














    If you need to output the array of created documents, the easest way will be to use find. But if you need just to a count of the new elements, and they has same structure, you can use a trick with update operator. That will looks like update({ id: { $nin: arrayOfId }}, { $set: newDoc }, { upsert: true }), where newDoc is a structure of the new doc, and arrayOfId is an array of you id (you can work with _id same way). This operation returns a count of new documents.



    And if you try to make a request from angular before component created, use the preloader. And, after you get the result - show it to user. Hope this will help you.






    share|improve this answer


























      0














      If you need to output the array of created documents, the easest way will be to use find. But if you need just to a count of the new elements, and they has same structure, you can use a trick with update operator. That will looks like update({ id: { $nin: arrayOfId }}, { $set: newDoc }, { upsert: true }), where newDoc is a structure of the new doc, and arrayOfId is an array of you id (you can work with _id same way). This operation returns a count of new documents.



      And if you try to make a request from angular before component created, use the preloader. And, after you get the result - show it to user. Hope this will help you.






      share|improve this answer
























        0












        0








        0






        If you need to output the array of created documents, the easest way will be to use find. But if you need just to a count of the new elements, and they has same structure, you can use a trick with update operator. That will looks like update({ id: { $nin: arrayOfId }}, { $set: newDoc }, { upsert: true }), where newDoc is a structure of the new doc, and arrayOfId is an array of you id (you can work with _id same way). This operation returns a count of new documents.



        And if you try to make a request from angular before component created, use the preloader. And, after you get the result - show it to user. Hope this will help you.






        share|improve this answer












        If you need to output the array of created documents, the easest way will be to use find. But if you need just to a count of the new elements, and they has same structure, you can use a trick with update operator. That will looks like update({ id: { $nin: arrayOfId }}, { $set: newDoc }, { upsert: true }), where newDoc is a structure of the new doc, and arrayOfId is an array of you id (you can work with _id same way). This operation returns a count of new documents.



        And if you try to make a request from angular before component created, use the preloader. And, after you get the result - show it to user. Hope this will help you.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 21:28









        Mikita Melnikau

        329313




        329313






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401296%2fhow-do-i-achieve-my-desired-functionality-using-node-js%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python