How to check if a document il linked to another in MongoDB [duplicate]2019 Community Moderator ElectionWhy 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?

Multi tool use
Multi tool use

How could a scammer know the apps on my phone / iTunes account?

Is it normal that my co-workers at a fitness company criticize my food choices?

Why Choose Less Effective Armour Types?

How to deal with taxi scam when on vacation?

What is "focus distance lower/upper" and how is it different from depth of field?

What is the purpose or proof behind chain rule?

Equivalents to the present tense

This word with a lot of past tenses

Happy pi day, everyone!

Can I use USB data pins as power source

Why does a Star of David appear at a rally with Francisco Franco?

If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

Are all passive ability checks floors for active ability checks?

Counting models satisfying a boolean formula

Shortcut for setting origin to vertex

Describing a chess game in a novel

What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?

A single argument pattern definition applies to multiple-argument patterns?

ERC721: How to get the owned tokens of an address

What is a ^ b and (a & b) << 1?

New passport but visa is in old (lost) passport

What is the adequate fee for a reveal operation?

As a new Ubuntu desktop 18.04 LTS user, do I need to use ufw for a firewall or is iptables sufficient?



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



2019 Community Moderator ElectionWhy 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) 
let token = await Token.findOne( token: req.body.token );

console.log(token._userId);
console.log(user._id);
console.log(token._userId != user._id);

if (token == null );


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) 
    let token = await Token.findOne( token: req.body.token );

    console.log(token._userId);
    console.log(user._id);
    console.log(token._userId != user._id);

    if (token == null );


    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) 
      let token = await Token.findOne( token: req.body.token );

      console.log(token._userId);
      console.log(user._id);
      console.log(token._userId != user._id);

      if (token == null );


      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) 
      let token = await Token.findOne( token: req.body.token );

      console.log(token._userId);
      console.log(user._id);
      console.log(token._userId != user._id);

      if (token == null );


      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,09922039




          4,09922039












          • 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





          O4jd1S3kOeswkC
          tO4u14xhs75a2bWA Spx17kt o3jRrU Vm7y7kxGUDnub,WfG,Ncu

          Popular posts from this blog

          Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

          2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived