How to check if a document il linked to another in MongoDB [duplicate] The Next CEO of Stack OverflowWhy do my MongooseJS ObjectIds fail the equality test?MongoDB vs. CassandraHow to query MongoDB with “like”?How do I pass command line arguments to a Node.js program?Check synchronously if file/directory exists in Node.jsHow to decide when to use Node.js?How to exit in Node.jsHow to get GET (query string) variables in Express.js on Node.js?How do I drop a MongoDB database from the command line?Find MongoDB records where array field is not emptyHow do I update each dependency in package.json to the latest version?

Why am I allowed to create multiple unique pointers from a single object?

Is HostGator storing my password in plaintext?

Can you replace a racial trait cantrip when leveling up?

Indicator light circuit

What is ( CFMCC ) on ILS approach chart?

Anatomically Correct Strange Women In Ponds Distributing Swords

If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?

How did people program for Consoles with multiple CPUs?

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

What is the purpose of the Evocation wizard's Potent Cantrip feature?

Inappropriate reference requests from Journal reviewers

How to invert MapIndexed on a ragged structure? How to construct a tree from rules?

Won the lottery - how do I keep the money?

Is there a way to save my career from absolute disaster?

How do scammers retract money, while you can’t?

Limits on contract work without pre-agreed price/contract (UK)

Should I tutor a student who I know has cheated on their homework?

Received an invoice from my ex-employer billing me for training; how to handle?

Does it take more energy to get to Venus or to Mars?

Why does standard notation not preserve intervals (visually)

Do I need to enable Dev Hub in my PROD Org?

Written every which way

Can I run my washing machine drain line into a condensate pump so it drains better?

If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?



How to check if a document il linked to another in MongoDB [duplicate]



The Next CEO of Stack OverflowWhy do my MongooseJS ObjectIds fail the equality test?MongoDB vs. CassandraHow to query MongoDB with “like”?How do I pass command line arguments to a Node.js program?Check synchronously if file/directory exists in Node.jsHow to decide when to use Node.js?How to exit in Node.jsHow to get GET (query string) variables in Express.js on Node.js?How do I drop a MongoDB database from the command line?Find MongoDB records where array field is not emptyHow do I update each dependency in package.json to the latest version?










0
















This question already has an answer here:



  • Why do my MongooseJS ObjectIds fail the equality test?

    1 answer



Suppose that I have two collections:



Users



_id:5c810da8c714a02e84d16d16
username:foo


Tokens



_id: 5c81347b00370c2848db8725
_userId: 5c810da8c714a02e84d16d16
token: "83790bf08fa16eec1c3c6761d0c1be4f"


I'm trying to check if the token with id 5c81347b00370c2848db8725 is linked to the user with id 5c810da8c714a02e84d16d16.



The first problem is that I only have the username to find the user details, so this is my solution:



User.findOne( username: req.body.username , async function (err, user) );


now the code above will return the following result:



5c810da8c714a02e84d16d16
5c810da8c714a02e84d16d16
true


but should return false because the record are equal.. What I did wrong?










share|improve this question













marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 7 at 20:39


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    0
















    This question already has an answer here:



    • Why do my MongooseJS ObjectIds fail the equality test?

      1 answer



    Suppose that I have two collections:



    Users



    _id:5c810da8c714a02e84d16d16
    username:foo


    Tokens



    _id: 5c81347b00370c2848db8725
    _userId: 5c810da8c714a02e84d16d16
    token: "83790bf08fa16eec1c3c6761d0c1be4f"


    I'm trying to check if the token with id 5c81347b00370c2848db8725 is linked to the user with id 5c810da8c714a02e84d16d16.



    The first problem is that I only have the username to find the user details, so this is my solution:



    User.findOne( username: req.body.username , async function (err, user) );


    now the code above will return the following result:



    5c810da8c714a02e84d16d16
    5c810da8c714a02e84d16d16
    true


    but should return false because the record are equal.. What I did wrong?










    share|improve this question













    marked as duplicate by Neil Lunn mongodb
    Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

    StackExchange.ready(function()
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function()
    $hover.showInfoMessage('',
    messageElement: $msg.clone().show(),
    transient: false,
    position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
    dismissable: false,
    relativeToBody: true
    );
    ,
    function()
    StackExchange.helpers.removeMessages();

    );
    );
    );
    Mar 7 at 20:39


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      0












      0








      0









      This question already has an answer here:



      • Why do my MongooseJS ObjectIds fail the equality test?

        1 answer



      Suppose that I have two collections:



      Users



      _id:5c810da8c714a02e84d16d16
      username:foo


      Tokens



      _id: 5c81347b00370c2848db8725
      _userId: 5c810da8c714a02e84d16d16
      token: "83790bf08fa16eec1c3c6761d0c1be4f"


      I'm trying to check if the token with id 5c81347b00370c2848db8725 is linked to the user with id 5c810da8c714a02e84d16d16.



      The first problem is that I only have the username to find the user details, so this is my solution:



      User.findOne( username: req.body.username , async function (err, user) );


      now the code above will return the following result:



      5c810da8c714a02e84d16d16
      5c810da8c714a02e84d16d16
      true


      but should return false because the record are equal.. What I did wrong?










      share|improve this question















      This question already has an answer here:



      • Why do my MongooseJS ObjectIds fail the equality test?

        1 answer



      Suppose that I have two collections:



      Users



      _id:5c810da8c714a02e84d16d16
      username:foo


      Tokens



      _id: 5c81347b00370c2848db8725
      _userId: 5c810da8c714a02e84d16d16
      token: "83790bf08fa16eec1c3c6761d0c1be4f"


      I'm trying to check if the token with id 5c81347b00370c2848db8725 is linked to the user with id 5c810da8c714a02e84d16d16.



      The first problem is that I only have the username to find the user details, so this is my solution:



      User.findOne( username: req.body.username , async function (err, user) );


      now the code above will return the following result:



      5c810da8c714a02e84d16d16
      5c810da8c714a02e84d16d16
      true


      but should return false because the record are equal.. What I did wrong?





      This question already has an answer here:



      • Why do my MongooseJS ObjectIds fail the equality test?

        1 answer







      node.js mongodb express mongoose






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 7 at 15:28









      teresteres

      1257




      1257




      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 7 at 20:39


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function()
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function()
      $hover.showInfoMessage('',
      messageElement: $msg.clone().show(),
      transient: false,
      position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
      dismissable: false,
      relativeToBody: true
      );
      ,
      function()
      StackExchange.helpers.removeMessages();

      );
      );
      );
      Mar 7 at 20:39


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The equality operator is mostly useful for comparing native types, such as strings. In MongoDB, the ObjectID is an object that cannot be compared with == operator.



          Mongoose uses the mongodb-native driver. These use the native ObjectID type from the Mongodb driver. Object IDs can be compared using the .equals() method.



          With your example, you should write your check as:



          user._id.equals(token._userId)


          Documentation



          Note that if you want to use Equal, you should convert the ObjectIDs to strings prior to making the comparison:



          user._id.toString() === token._userId.toString()





          share|improve this answer

























          • ok so essentially mine solution didn't works because the object are not string but object of mongo

            – teres
            Mar 7 at 15:45











          • That is correct

            – Pierre
            Mar 7 at 15:46

















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The equality operator is mostly useful for comparing native types, such as strings. In MongoDB, the ObjectID is an object that cannot be compared with == operator.



          Mongoose uses the mongodb-native driver. These use the native ObjectID type from the Mongodb driver. Object IDs can be compared using the .equals() method.



          With your example, you should write your check as:



          user._id.equals(token._userId)


          Documentation



          Note that if you want to use Equal, you should convert the ObjectIDs to strings prior to making the comparison:



          user._id.toString() === token._userId.toString()





          share|improve this answer

























          • ok so essentially mine solution didn't works because the object are not string but object of mongo

            – teres
            Mar 7 at 15:45











          • That is correct

            – Pierre
            Mar 7 at 15:46















          1














          The equality operator is mostly useful for comparing native types, such as strings. In MongoDB, the ObjectID is an object that cannot be compared with == operator.



          Mongoose uses the mongodb-native driver. These use the native ObjectID type from the Mongodb driver. Object IDs can be compared using the .equals() method.



          With your example, you should write your check as:



          user._id.equals(token._userId)


          Documentation



          Note that if you want to use Equal, you should convert the ObjectIDs to strings prior to making the comparison:



          user._id.toString() === token._userId.toString()





          share|improve this answer

























          • ok so essentially mine solution didn't works because the object are not string but object of mongo

            – teres
            Mar 7 at 15:45











          • That is correct

            – Pierre
            Mar 7 at 15:46













          1












          1








          1







          The equality operator is mostly useful for comparing native types, such as strings. In MongoDB, the ObjectID is an object that cannot be compared with == operator.



          Mongoose uses the mongodb-native driver. These use the native ObjectID type from the Mongodb driver. Object IDs can be compared using the .equals() method.



          With your example, you should write your check as:



          user._id.equals(token._userId)


          Documentation



          Note that if you want to use Equal, you should convert the ObjectIDs to strings prior to making the comparison:



          user._id.toString() === token._userId.toString()





          share|improve this answer















          The equality operator is mostly useful for comparing native types, such as strings. In MongoDB, the ObjectID is an object that cannot be compared with == operator.



          Mongoose uses the mongodb-native driver. These use the native ObjectID type from the Mongodb driver. Object IDs can be compared using the .equals() method.



          With your example, you should write your check as:



          user._id.equals(token._userId)


          Documentation



          Note that if you want to use Equal, you should convert the ObjectIDs to strings prior to making the comparison:



          user._id.toString() === token._userId.toString()






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 7 at 15:46

























          answered Mar 7 at 15:42









          PierrePierre

          4,10422039




          4,10422039












          • ok so essentially mine solution didn't works because the object are not string but object of mongo

            – teres
            Mar 7 at 15:45











          • That is correct

            – Pierre
            Mar 7 at 15:46

















          • ok so essentially mine solution didn't works because the object are not string but object of mongo

            – teres
            Mar 7 at 15:45











          • That is correct

            – Pierre
            Mar 7 at 15:46
















          ok so essentially mine solution didn't works because the object are not string but object of mongo

          – teres
          Mar 7 at 15:45





          ok so essentially mine solution didn't works because the object are not string but object of mongo

          – teres
          Mar 7 at 15:45













          That is correct

          – Pierre
          Mar 7 at 15:46





          That is correct

          – Pierre
          Mar 7 at 15:46





          Popular posts from this blog

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved