Javascript 将objectID或_id从MongoDB Stitch查询转换为字符串并映射它
我可以查询MongoDB Stitch数据库,但得到的结果不同 如果我将所有数据都记录在控制台中,它看起来是这样的(注意'id:Uint8Array(12)[94,67,83,…]'Javascript 将objectID或_id从MongoDB Stitch查询转换为字符串并映射它,javascript,arrays,json,reactjs,mongodb,Javascript,Arrays,Json,Reactjs,Mongodb,我可以查询MongoDB Stitch数据库,但得到的结果不同 如果我将所有数据都记录在控制台中,它看起来是这样的(注意'id:Uint8Array(12)[94,67,83,…]' item.find().asArray() .then(data => { console.log("Found docs", data) }); 但是,如果我将其作为数组查询: // Find database documents item.find({})
item.find().asArray()
.then(data => {
console.log("Found docs", data)
});
但是,如果我将其作为数组查询:
// Find database documents
item.find({})
.toArray()
.then(xyz =>
this.setState({xyz})
)
以下是我收到的数据,并说明:
{
"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…}"
]
}
我想获取objectID(应该类似于“5e435320e45ce24954381c52”),它似乎是“bsonType”的唯一键:“objectID”模式,我假设我需要将其转换为字符串(?),这样我就可以映射它并将其用作表中稍后使用的唯一键(“列表中的每个子级都应该有一个唯一的“key”属性”)
谢谢!已经是一个好消息了
要获取字符串值,只需使用toString()
item.find().toArray()
.then(data => {
console.log(data.map(elem => elem._id.toString()));
});