[SERVER-9677] $db values for DBRef datatype does not seem to persist in mongo collection Created: 14/May/13  Updated: 07/Mar/14  Resolved: 28/May/13

Status: Closed
Project: Core Server
Component/s: Storage, Write Ops
Affects Version/s: 2.4.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Ravi Shakya Assignee: Stephen Lee
Resolution: Duplicate Votes: 0
Labels: BSON, DBRef
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 10.04 on x86


Attachments: PNG File document_stored.png     PNG File insert_DBRef.png    
Issue Links:
Duplicate
duplicates SERVER-9790 DBRef string representation should in... Closed
Operating System: Linux
Steps To Reproduce:

1) Create a collection named 'people' in database 'kamal' and insert a doc as
db.people.insert({_id : ObjectId('5191cc6684ae75ea07e4ef3b'), "name" : "Tu"})
2) Now in another database, insert a document into collection named 'halloa' as follows:
db.halloa.insert({"name" : "dukur", "myref" :

{"$ref" : "people", "$id" : "5191cc6684ae75ea07e4ef3b", "$db" : "kamal"}

})
3) Retrieve the document using following query:
db.halloa.find(

{"name" : "dukur"}

)

Participants:

 Description   

While storing a DBRef datatype with all the 3 values i.e. $ref, $id and $db values into a collection, $db does not seem to have been stored.
When i retrieve the doc, I could see $ref and $id values but not $db values. Please refer to the attached screenshots.



 Comments   
Comment by Jérôme Leleu [ 17/Jun/13 ]

Hi,

Any news for this ?
Thx.
Best,
Jérôme

Comment by Stephen Lee [ 28/May/13 ]

Ravi, I'm resolving this. Please upvote/watch SERVER-9790 for the inclusion of $db in the DBRef string representation.

Comment by Ravi Shakya [ 24/May/13 ]

Yeah Steven if you could create improvement ticket and if it's fixed, everyone will benefit I believe.
Thank you

Comment by Stephen Lee [ 24/May/13 ]

Ravi,

Yes, I agree it's a little confusing that the string representation of the DBRef doesn't include the referenced database. However, it should be pretty clear that DBRef object does retain the referenced database.

If you're interested, I can create an improvement ticket for you.

-Stephen

Comment by Ravi Shakya [ 24/May/13 ]

Hi Stephen, Thanks for checking and informing about the issue. Yeah I expected $db value to be present in BSON doc just like others i.e. $id and $ref. I wonder why it was not displayed on BSON when other 2 were.

Comment by Stephen Lee [ 23/May/13 ]

Hi Ravi,

Based on your screenshots, I suspect that you're expecting the database to appear in the BSON to string representation of the DBRef. However, upon inspection of the $db field of the objected stored in "myref" field, I do see the expected database name, "kamal".

MongoDB shell version: 2.4.3
connecting to: 127.0.0.1:20000/test
> use kamal
switched to db kamal
> db.people.insert({_id : ObjectId('5191cc6684ae75ea07e4ef3b'), "name" : "Tu"})
> use test
switched to db test
> db.halloa.insert({"name" : "dukur", "myref" : {"$ref" : "people", "$id" : "5191cc6684ae75ea07e4ef3b", "$db" : "kamal"} })
> db.halloa.find({"name" : "dukur"})
{ "_id" : ObjectId("519e5c065ef254863b9051c6"), "name" : "dukur", "myref" : DBRef("people", "5191cc6684ae75ea07e4ef3b") }
> z=db.halloa.find({"name" : "dukur"}).toArray()[0]
{
	"_id" : ObjectId("519e5c065ef254863b9051c6"),
	"name" : "dukur",
	"myref" : DBRef("people", "5191cc6684ae75ea07e4ef3b")
}
> z.myref.$id
5191cc6684ae75ea07e4ef3b
> z.myref.$ref
people
> z.myref.$db
kamal

As an aside, we generally recommend denormalizing your data such that you won't need references, but if you must "join" two collections, a manual reference might be more flexible.

-Stephen

Generated at Thu Feb 08 03:21:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.