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?
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?
node.js mongodb express mongoose
marked as duplicate by Neil Lunn
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.
add a comment |
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?
node.js mongodb express mongoose
marked as duplicate by Neil Lunn
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.
add a comment |
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?
node.js mongodb express mongoose
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
node.js mongodb express mongoose
asked Mar 7 at 15:28
teresteres
1257
1257
marked as duplicate by Neil Lunn
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
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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()
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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()
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
add a comment |
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()
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
add a comment |
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()
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()
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
add a comment |
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
add a comment |