我有一個Node.js應用程式,拉一些數據,它堅持到一個對象,像這樣:比較貓鼬_id和字符串
var results = new Object();
User.findOne(query, function(err, u) {
results.userId = u._id;
}
當我做了,如果再根據這個存儲的ID /時,
if (results.userId == AnotherMongoDocument._id) {
console.log('This is never true');
}
當我做兩個ID的的的console.log,嚴絲合縫:
User id: 4fc67871349bb7bf6a000002 AnotherMongoDocument id: 4fc67871349bb7bf6a000002
我一樣比較是不正確的總結這是一種數據類型的問題,但我不知道如何將results.userId轉換爲數據類型,這將導致上述比較爲真,我的外包大腦(又名Google)一直無法提供幫助。
「.equals()」的文檔:http://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html#equals – 2015-03-06 13:31:42
如果您已經在使用'mongoose'可以'require('mongoose')。mongo.ObjectID',所以你不必列出任何額外的依賴 – JoshuaDavid 2017-07-14 19:19:10