英文:
MongoDB - DB Reference returns null value
问题
以下是您要翻译的内容:
如图所示,当我尝试使用MongoDB中的DB引用从集合'address_home'中检索地址详细信息时,它返回null值。有人知道这段代码有什么问题吗?
我尝试了图像中的代码。我希望它在用户表中打印出'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,如图所示。
我尝试了这段代码,链接在这里:
https://www.tutorialspoint.com/mongodb/mongodb_database_references.htm
英文:
As seen in this image when I try to retrieve address details from collection 'address_home' using DB Reference in MongoDB, it returns null value. Anybody know what is the problem with this code?
I tried the code in the image. I want it to print data from the 'address_home' collection in place of the 'DBRef()' data on users table.
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
and finally,
db[dbRef.$ref].findOne({"_id":(dbRef.$id)})
but, this command returns null as seen in the image.
I tried this code from here:
https://www.tutorialspoint.com/mongodb/mongodb_database_references.htm
答案1
得分: 0
从数据类型文档中:
> 与传统的Mongo Shell相比,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
属性:
名称 | 类型 | 描述 |
---|---|---|
collection | string | 集合名称。 |
oid | ObjectId | 引用的ObjectId。 |
db | string | 可选的数据库名称,如果省略,则引用局限于当前数据库。 |
user.address
是DBRef
类的一个实例:
Atlas atlas-zy5qpa-shard-0 [主要] test> user.address instanceof DBRef
true
因此,mongosh
中的命令应为:
Atlas atlas-zy5qpa-shard-0 [主要] test> db[user.address.collection].findOne({_id: user.address.oid})
{
_id: ObjectId("534009e4d852427820000002"),
building: '22 A, Indiana Apt',
pincode: 123456,
city: '洛杉矶',
state: '加利福尼亚'
}
或者,使用toJSON()方法获取BSON.DBRefLike数据。
Atlas atlas-zy5qpa-shard-0 [主要] test> dbRef.toJSON()
{
'$ref': 'address_home',
'$id': ObjectId("534009e4d852427820000002"),
'$db': 'test'
}
Atlas atlas-zy5qpa-shard-0 [主要] test> db[dbRef.toJSON().$ref].findOne({_id: dbRef.toJSON().$id})
{
_id: ObjectId("534009e4d852427820000002"),
building: '22 A, Indiana Apt',
pincode: 123456,
city: '洛杉矶',
state: '加利福尼亚'
}
用户文档(注意:我使用的是test
数据库,而不是tutorialspoint
):
Atlas atlas-zy5qpa-shard-0 [主要] 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 [主要] test> db.version()
6.0.6
附注:在tutorialspoint网站的文档中甚至没有MongoDB和mongosh
版本信息。我猜我不会使用它。
英文:
From the Data Types documentation:
> Compared to the legacy mongo shell, MongoDB Shell(mongosh
) has type handling which is better aligned with the default types used by the MongoDB drivers.
For example, the constructor's signature of 5.6/classes/BSON.DBRef.html#constructor class in node-mongodb-driver:
new DBRef(collection: string, oid: ObjectId, db?: string, fields?: Document): DBRef
Properties:
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. |
The user.address
is an instance of DBRef
class:
Atlas atlas-zy5qpa-shard-0 [primary] test> user.address instanceof DBRef
true
So the command in mongosh
should be:
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'
}
Or, use toJSON() method to get the BSON.DBRefLike data.
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'
}
The user document(Note: I use test
database, not tutorialspoint
):
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
version:
$ mongosh --version
1.10.1
MongoDB version:
Atlas atlas-zy5qpa-shard-0 [primary] test> db.version()
6.0.6
P.S. There is not even MongoDB and mongosh
version information in tutorialspoint site's documentation. I guess I won't use it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论