Discussion:
how to reference and get document with MongoDbRef
Rakesh Sharma
2014-06-16 19:50:08 UTC
Permalink
Hi I have class structure like this
public class smilies
{
public ObjectId _id { get; set; }
public MongoDBRef smilies_category{ get; set; }
public string smil_url { get; set; }
public string smil_detail { get; set; }
}

public class smilies_category
{
public ObjectId _id { get; set; }
public string scat_name { get; set; }
}

and I am using below code for getting the record
var refDocument = new BsonDocument {
{"$ref", "smil_scat_id"},
{"$id", "539ef7c2e46b621314956e3b"}
};

var query = Query.EQ("smilies_category", refDocument);
var result = db.GetCollection("smilies").Find(query)



Now What I want here is combine result like
"_id" : ObjectId("539f3ec1e46b62120023d364"),
"scat_name : "Bussines",
"smil_url" : "www.gmail.com",
"smil_detail" : "Ok fine",

I have referenced smilies category in smiles to get the name of smilies
category

How can we achiev this in Mongo Db?

Is I am going right way?

Please let me know ..?
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/cd9cbaa6-b423-4115-9af0-d85faec2b89e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
William Berkeley
2014-07-07 15:01:48 UTC
Permalink
Hi Rakesh. According to the docs on Database References
<http://docs.mongodb.org/manual/reference/database-references/#DatabaseReferences-DBRef>, MongoDB
drivers, like the C# driver, generally do not automatically resolve DBRefs.
It is up to the application code to do that.

Instead of a denormalized schema with smilies_category documents in a
different collection, consider storing the smilies_category value directly
on the smilies documents:

public class smilies
{
public ObjectId _id { get; set; }
public string smilies_category{ get; set; }
public string smil_url { get; set; }
public string smil_detail { get; set; }
}

so the docs are stored in the database like

{
"_id" : ObjectId("539f3ec1e46b62120023d364"),
"scat_name" : "Bussines",
"smil_url" : "www.gmail.com",
"smil_detail" : "Ok fine",
}

Whether it's appropriate or not to do this depends on your application's
data access patterns. It looks appropriate for this situation to me because
one smilies document will have one category and queries will either want to
simply return the category with the smilies document or search for all of
the smilies documents that have a given category. The Data Modeling
Introduction
<http://docs.mongodb.org/manual/core/data-modeling-introduction/> is a good
place to start for more information about making good schema design
choices. If you have any follow up questions, please don't hesitate to ask.

-Will
Post by Rakesh Sharma
Hi I have class structure like this
public class smilies
{
public ObjectId _id { get; set; }
public MongoDBRef smilies_category{ get; set; }
public string smil_url { get; set; }
public string smil_detail { get; set; }
}
public class smilies_category
{
public ObjectId _id { get; set; }
public string scat_name { get; set; }
}
and I am using below code for getting the record
var refDocument = new BsonDocument {
{"$ref", "smil_scat_id"},
{"$id", "539ef7c2e46b621314956e3b"}
};
var query = Query.EQ("smilies_category", refDocument);
var result = db.GetCollection("smilies").Find(query)
Now What I want here is combine result like
"_id" : ObjectId("539f3ec1e46b62120023d364"),
"scat_name : "Bussines",
"smil_url" : "www.gmail.com",
"smil_detail" : "Ok fine",
I have referenced smilies category in smiles to get the name of smilies
category
How can we achiev this in Mongo Db?
Is I am going right way?
Please let me know ..?
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
To post to this group, send email to mongodb-user-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/319854d5-fbdd-486f-a98d-a453116269d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...