简体   繁体   English

将 objectID 或 _id 从 MongoDB Stitch 查询转换为字符串和 map

[英]Convert objectID or _id from MongoDB Stitch query to string and map it

I am able to query a MongoDB Stitch database, but I get different results.我可以查询 MongoDB Stitch 数据库,但我得到了不同的结果。

If I console log all my data, it appears like this (note the 'id: Uint8Array(12) [ 94, 67, 83, … ]'如果我控制台记录我所有的数据,它看起来像这样(注意 'id: Uint8Array(12) [ 94, 67, 83, … ]'

item.find().asArray()
.then(data => {
    console.log("Found docs", data)
  });

However if I query it as an array:但是,如果我将其作为数组查询:

      // Find database documents
      item.find({})
      .toArray()
      .then(xyz => 
        this.setState({xyz})
      )

Here is the data I receive, and state:这是我收到的数据,state:

{
    "xyz": [
      {
        "_id": {
          "id": "DataView(12)",
          "get_inc": "",
          "getInc": "",
          "generate": "",
          "generationTime": 1581470496,
          "inspect": "toString"
        },
        "company": "AAA",
        "url": "http://www.google.com",
        "loc": "sac, ca",
        "gender": "female",
        "tags": "clothes"
      },
      "{_id: {…}, company: \"BBB\", gender: \"male\", loc: \"sf…}",
      "{_id: {…}, company: \"ZZZ\", gender: \"male\", loc: \"oa…}"
    ]
  }

I would like to grab the objectID (which should be something like "5e435320e45ce24954381c52") which appears to be a unique key of a "bsonType": "objectId" schema, and I assume I need it converted as a string (?), so I can map it and have it serve as a unique key for use later in a table ("Each child in a list should have a unique "key" prop.").我想获取 objectID(应该类似于“5e435320e45ce24954381c52”),它似乎是“bsonType”的唯一键:“objectId”模式,我假设我需要将它转换为字符串(?),所以我可以 map 它并让它作为一个唯一的键,供稍后在表中使用(“列表中的每个孩子都应该有一个唯一的“键”道具。”)。

Thanks!谢谢!

Already is an ObjectId .已经是一个ObjectId

To get the string value just use toString()要获取字符串值,只需使用toString()

item.find().toArray()
.then(data => {
    console.log(data.map(elem => elem._id.toString()));
});

暂无
暂无

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

相关问题 将 ObjectID (Mongodb) 转换为 JavaScript 中的字符串 - Convert ObjectID (Mongodb) to String in JavaScript 如何将字符串转换为 mongodb ObjectId? - How to convert a string to a mongodb ObjectId? 无法在 MongoDb forEach 函数中将 ObjectId 转换为字符串 - cannot convert ObjectId to String in MongoDb forEach function MongoDB 查询聚合 localField ObjectId 字符串和外部字段 ObjectId - MongoDB query aggregation localField ObjectId string and foreign field ObjectId 如何在mongodb中使用_id(字符串)作为_id(objectId)导入json文件? - How to import a json file with _id (string) as _id (objectId) in mongodb? 使用 MongoDB 聚合时出现错误,“无法从 BSON 类型 objectId 转换为 String” - Getting an error, 'Can't convert from BSON type objectId to String' when using MongoDB aggregation MongoDB无法更新文档,因为_id是字符串,而不是ObjectId - MongoDB can't update document because _id is string, not ObjectId 如何在nodejs mongodb本机驱动程序中将字符串转换为ObjectId? - How to convert a string to ObjectId in nodejs mongodb native driver? 使用 MongoDB Stitch webhook 服务功能,如何将返回的“insertedId”转换为字符串? - Using MongoDB Stitch webhook service function, how can I convert the returned "insertedId" to string? 如何使用 node.js 将 _bsontype objectID 值转换为 string/objectID 以插入到 mongoDB 中 - How to convert _bsontype objectID value to string/objectID to insert in mongoDB using node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM