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?

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





          Popular posts from this blog

          Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

          Can't find variable: document ReactNative Js2019 Community Moderator ElectionReactNative: how to center text?Can't find variable: navigate - ReactNative navigationReact Native error: “Can't find variable: TouchableHighlight”How to use Expo.DocumentPicker.getDocumentAsync properly. [Expo] [ReactNative]Can't find variable: regeneratorRuntime (ReactNative)Reactnative Expo It is advisable for a StartUp?Can't find variable : Self / Can't install firebaseNPM Install Error in installing ReactNative CLIContacts Displaying in App using ReactnativeFacebook login error: can't find variable: Alert

          Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices