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 badges2 Answers
Reset to default 7This 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 badges2 Answers
Reset to default 7This 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
).
本文标签:
版权声明:本文标题:javascript - NodeJS, using MongoDB Native driver, how do I convert ObjectID to string - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744448312a2097533.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论