最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - NodeJS, using MongoDB Native driver, how do I convert ObjectID to string - Stack Overflow

matteradmin6PV0评论

I am using MongoDB native driver for NodeJS, and am having trouble converting ObjectID to a string.

My code looks like:

db.collection('user', function(err, collection) {
  collection.insert(data, {safe:true}, function(err, result) { 
    var myid = result._id.toString();
    console.log(myid);
  )};
});

I have tried various suggestions on StackOverflow like:

myid = result._id.toString();
myid = result._id.toHexString();

but none of them seemed to work.

I am trying to convert the ObjectID to base64 encoding.

Not sure if I am running into supported functionality under the Mongo native driver.

I am using MongoDB native driver for NodeJS, and am having trouble converting ObjectID to a string.

My code looks like:

db.collection('user', function(err, collection) {
  collection.insert(data, {safe:true}, function(err, result) { 
    var myid = result._id.toString();
    console.log(myid);
  )};
});

I have tried various suggestions on StackOverflow like:

myid = result._id.toString();
myid = result._id.toHexString();

but none of them seemed to work.

I am trying to convert the ObjectID to base64 encoding.

Not sure if I am running into supported functionality under the Mongo native driver.

Share Improve this question edited Aug 21, 2013 at 2:27 Diosney 10.6k15 gold badges68 silver badges113 bronze badges asked Aug 21, 2013 at 2:02 ArcaneArcane 1172 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

This work for me:

var ObjectID = require('mongodb').ObjectID;
var idString = '4e4e1638c85e808431000003';
var idObj = new ObjectID(idString);

console.log(idObj);
console.log(idObj.toString());
console.log(idObj.toHexString());

Output:

4e4e1638c85e808431000003
4e4e1638c85e808431000003
4e4e1638c85e808431000003

insert returns an array of results (as you can also send an array of objects to be inserted), so your code is trying to get the _id from the array instance rather than the first result:

MongoClient.connect("mongodb://localhost:27017/testdb", function(err, db) {
    db.collection("user").insert({name:'wiredprairie'}, function(err, result) {
        if (result && result.length > 0) {
            var myid = result[0]._id.toString();
            console.log(myid);
        }
    });
});

Also, you won't need to base64 encode the result of calling toString on an ObjectId as it's returned as a hex number already. You could also call: result[0]._id.toHexString() to get the Hex value directly (toString just wraps toHexString).

Post a comment

comment list (0)

  1. No comments so far