cursor.showRecordId()
定义
cursor.showRecordId()
重要
mongosh 方法
这是一个
mongosh
方法。 这不是Node.js
或其他特定于编程语言的驱动程序方法的文档。在大多数情况下,
mongosh
方法的工作方式与传统mongo
shell 方法相同。但是,某些旧方法在mongosh
中不可用。有关旧版
mongo
shell 文档,请参阅相应 MongoDB Server 版本的文档:对于 MongoDB API 驱动程序,请参阅特定语言的MongoDB 驱动程序文档。
将
$recordId
字段附加到查询返回的文档。$recordId
是唯一标识集合中文档的内部密钥。$recordId
格式:'$recordId': Long(<int>) 返回: 修改后的游标对象,包含文档字段和附加的 $recordId
字段。
例子
该示例使用此pizzas
集合:
db.pizzas.insertMany( [ { type: "pepperoni", size: "small", price: 4 }, { type: "cheese", size: "medium", price: 7 }, { type: "vegan", size: "large", price: 8 } ] )
以下find()
示例使用showRecordId()
将$recordId
附加到输出中的pizza
文档字段:
db.pizzas.find().showRecordId()
示例输出:
[ { _id: ObjectId("62ffc70660b33b68e8f30435"), type: 'pepperoni', size: 'small', price: 4, '$recordId': Long("1") }, { _id: ObjectId("62ffc70660b33b68e8f30436"), type: 'cheese', size: 'medium', price: 7, '$recordId': Long("2") }, { _id: ObjectId("62ffc70660b33b68e8f30437"), type: 'vegan', size: 'large', price: 8, '$recordId': Long("3") } ]