admin管理员组

文章数量:1025517

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).

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).

本文标签: