{"id":565,"date":"2017-01-03T15:02:42","date_gmt":"2017-01-03T13:02:42","guid":{"rendered":"http:\/\/www.psinke.nl\/blog\/?p=565"},"modified":"2017-01-03T15:02:42","modified_gmt":"2017-01-03T13:02:42","slug":"converting-a-hexstring-to-objectid-for-mongodb-in-javascript","status":"publish","type":"post","link":"https:\/\/www.psinke.nl\/blog\/converting-a-hexstring-to-objectid-for-mongodb-in-javascript\/","title":{"rendered":"Converting a hexstring to ObjectId for MongoDB in Javascript"},"content":{"rendered":"<div class=\"afa2b3b851443650502c43e85c746c1f\" data-index=\"1\" style=\"float: none; margin:0px;\">\n<script type=\"text\/javascript\"><!--\r\ngoogle_ad_client = \"ca-pub-4223201538046844\";\r\n\/* standaardblok *\/\r\ngoogle_ad_slot = \"9522527129\";\r\ngoogle_ad_width = 320;\r\ngoogle_ad_height = 50;\r\n\/\/-->\r\n<\/script>\r\n<script type=\"text\/javascript\"\r\nsrc=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\r\n<\/script>\n<\/div>\n<h2>How to convert a string value to ObjectID<\/h2>\n<p>Sometimes, the ObjectID of your MongoDB document is delivered as a 24 character hexstring. You can&#8217;t just compare this hexstring to an ObjectID, because the comparison will fail.<\/p>\n<p>The solution is as follows:<\/p>\n<pre>var mongoose = require('mongoose' );\r\nvar hexstring = '58666c89d4bc6622ed5373dc';\r\n\/\/ convert it to ObjectID\r\nvar ObjIDtype = mongoose.Types.ObjectId(hexstring);<\/pre>\n<p>The variable ObjIDtype now contains the value as type ObjectID and can be compared to MongoDB objectID&#8217;s.<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>How to convert a string value to ObjectID Sometimes, the ObjectID of your MongoDB document is delivered as a 24 character hexstring. You can&#8217;t just compare this hexstring to an ObjectID, because the comparison will fail. The solution is as follows: var mongoose = require(&#8216;mongoose&#8217; ); var hexstring = &#8216;58666c89d4bc6622ed5373dc&#8217;; \/\/ convert it to ObjectID&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[192],"tags":[446,142,431,448,447],"class_list":["post-565","post","type-post","status-publish","format-standard","hentry","category-web-software-development","tag-hexstring","tag-javascript","tag-mongodb","tag-mongoose","tag-objectid"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/posts\/565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/comments?post=565"}],"version-history":[{"count":1,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/posts\/565\/revisions"}],"predecessor-version":[{"id":566,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/posts\/565\/revisions\/566"}],"wp:attachment":[{"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/media?parent=565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/categories?post=565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.psinke.nl\/blog\/wp-json\/wp\/v2\/tags?post=565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}