简体   繁体   English

NodeJS,使用MongoDB Native驱动程序,如何将ObjectID转换为字符串

[英]NodeJS, using MongoDB Native driver, how do I convert ObjectID to string

I am using MongoDB native driver for NodeJS, and am having trouble converting ObjectID to a string . 我正在为NodeJS使用MongoDB本机驱动程序,并且无法将ObjectID转换为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: 我在StackOverflow上尝试了各种建议,如:

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. 我试图将ObjectID转换为base64编码。

Not sure if I am running into supported functionality under the Mongo native driver. 不确定我是否遇到了Mongo本机驱动程序下的受支持功能。

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: insert返回一个结果数组(因为你也可以发送一个要插入的对象数组),所以你的代码试图从数组实例而不是第一个结果中获取_id

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. 此外,您不需要对ObjectId上调用toString的结果进行base64编码,因为它已经作为十六进制数返回。 You could also call: result[0]._id.toHexString() to get the Hex value directly ( toString just wraps toHexString ). 您还可以调用: result[0]._id.toHexString()直接获取Hex值( toString只包含toHexString )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在nodejs mongodb本机驱动程序中将字符串转换为ObjectId? - How to convert a string to ObjectId in nodejs mongodb native driver? 如何将字符串转换为 mongodb ObjectId? - How to convert a string to a mongodb ObjectId? 如何使用 node.js 将 _bsontype objectID 值转换为 string/objectID 以插入到 mongoDB 中 - How to convert _bsontype objectID value to string/objectID to insert in mongoDB using node.js 将 ObjectID (Mongodb) 转换为 JavaScript 中的字符串 - Convert ObjectID (Mongodb) to String in JavaScript 如何使用bluebird宣传MongoDB本机Javascript驱动程序? - How can I promisify the MongoDB native Javascript driver using bluebird? 如何将MongoDB查询捕获为字符串并将其显示在我的Node JS页面中(使用mongojs驱动程序)? - How do I capture a MongoDB query as a string and display it in my Node JS page (using the mongojs driver)? 无法在 MongoDb forEach 函数中将 ObjectId 转换为字符串 - cannot convert ObjectId to String in MongoDb forEach function 如何在 nodejs 中将 MongoDb ObjectId(“uniqueid”) 序列化为 JSON? - How serialize MongoDb ObjectId(“uniqueid”) to JSON in nodejs? NodeJS和MongoDB-ObjectId数组如何返回对象 - NodeJS and MongoDB - Array of ObjectId how to return Objects 使用节点 js 创建 Mongodb ObjectId 时如何捕获错误 - How do I catch error when creating a Mongodb ObjectId using node js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM