output

如本图所示,当我try 使用MongoDB中的DB引用从集合‘ADDRESS_HOME’检索地址详细信息时,它返回空值.有人知道这段代码有什么问题吗?

我try 了图像中的代码.我希望它打印‘ADDRESS_HOME’集合中的数据,而不是用户表上的‘DBRef()’数据.

use tutorialspoint

db.address_home.insertOne({ "_id": ObjectId("534009e4d852427820000002"), "building": "22 A, Indiana Apt", "pincode": 123456, "city": "Los Angeles", "state": "California" })

db.users.insertOne( { "_id": ObjectId("53402597d852426020000002"), "address": { "$ref": "address_home", "$id": ObjectId("534009e4d852427820000002"), "$db": "tutorialspoint" }, "contact": "987654321", "dob": "01-01-1991", "name": "Tom Benzamin" })

var user = db.users.findOne({"name":"Tom Benzamin"})
var dbRef = user.address

最后,

db[dbRef.$ref].findOne({"_id":(dbRef.$id)})

但是,该命令返回NULL,如图所示.

我在这里try 了这个代码: https://www.tutorialspoint.com/mongodb/mongodb_database_references.htm

推荐答案

Data Types个文档中:

与遗留的mongoshell 相比,MongoDB Shell(mongosh)的类型处理更符合MongoDB驱动程序使用的默认类型.

例如,NODE-MongoDB-DRIVER中5.6/classes/BSON.DBRef.html#constructor类的构造函数签名:

new DBRef(collection: string, oid: ObjectId, db?: string, fields?: Document): DBRef

属性:

name type description
collection string the collection name.
oid ObjectId the reference ObjectId.
db string optional db name, if omitted the reference is local to the current db.

user.addressDBRef类的实例:

Atlas atlas-zy5qpa-shard-0 [primary] test> user.address instanceof DBRef
true

因此,mongosh中的命令应该是:

Atlas atlas-zy5qpa-shard-0 [primary] test> db[user.address.collection].findOne({_id: user.address.oid})
{
  _id: ObjectId("534009e4d852427820000002"),
  building: '22 A, Indiana Apt',
  pincode: 123456,
  city: 'Los Angeles',
  state: 'California'
}

或者,使用toJSON()方法获取BSON.DBRefLike数据.

Atlas atlas-zy5qpa-shard-0 [primary] test> dbRef.toJSON()
{
  '$ref': 'address_home',
  '$id': ObjectId("534009e4d852427820000002"),
  '$db': 'test'
}
Atlas atlas-zy5qpa-shard-0 [primary] test> db[dbRef.toJSON().$ref].findOne({_id: dbRef.toJSON().$id})
{
  _id: ObjectId("534009e4d852427820000002"),
  building: '22 A, Indiana Apt',
  pincode: 123456,
  city: 'Los Angeles',
  state: 'California'
}

用户文档(Note: I use 100 database, not 101):

Atlas atlas-zy5qpa-shard-0 [primary] test> db.users.findOne({name: 'Tom Benzamin'})
{
  _id: ObjectId("53402597d852426020000002"),
  address: DBRef("address_home", ObjectId("534009e4d852427820000002"), "test"),
  contact: '987654321',
  dob: '01-01-1991',
  name: 'Tom Benzamin'
}

mongosh版本:

$ mongosh --version
1.10.1

MongoDB版本:

Atlas atlas-zy5qpa-shard-0 [primary] test> db.version()
6.0.6

附注:tutorialspoint站点的文档中甚至没有MongoDB和mongosh版本的信息.我想我不会用它了.

Mongodb相关问答推荐

我们可以在Mongoose中这样使用Unique:[True,";This to Unique&qot;]吗

Golang mongodb聚合错误:管道阶段规范对象必须包含一个字段

MongoDB:使用数组过滤器进行更新插入

MongoDB C# 驱动程序 2.0 InsertManyAsync 与 BulkWriteAsync

mongoDB vs mySQL - 为什么在某些方面比另一个更好

解析命令行时出错:unrecognized option --rest

.insertOne 不是函数

Java MongoDB FindOne 获取最后插入的记录

在 Mongoose 中清理用户输入

如何更新 mongodb 文档以向数组添加新元素?

MongoDB:单个数据库处理程序的 >5 个打开连接

MongoDB 日志(log)文件和 oplog 有何不同?

MongoDB 数据库,相当于 SELECT column1, column2 FROM tbl

使用 Spring Security + Spring 数据 + MongoDB 进行身份验证

从 id 删除 PyMongo 中的文档

MongoDB - Permission denied for socket: /tmp/mongodb-27017.sock

Mongodb Atlas:管理员未授权执行命令

多次使用位置 `$` 运算符来更新嵌套数组

带有索引字段的 MongoDB 正则表达式

MongoDB:删除唯一约束