[CSHARP-688] MongoDBRef getting from undefined type(with bsondocument) Created: 20/Feb/13  Updated: 20/Mar/14  Resolved: 05/Mar/13

Status: Closed
Project: C# Driver
Component/s: Feature Request
Affects Version/s: 1.7.1
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: ??????? ??????? ??????? Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

new driver method



 Description   

now I fetch dbref by following code:
BsonDocument doc = test.FindOne();
var l = doc.Elements.ElementAt(1);
var linkr=l.Value;//dbref there
MongoDBRefSerializer ser=new MongoDBRefSerializer();
BsonReader reader = BsonReader.Create(linkr.ToJson());
var refrefref= ser.Deserialize(reader, typeof(MongoDBRef), new opt());
MongoDBRef a = (MongoDBRef)refrefref;
var referenced = db.FetchDBRef(a);

opt class
class opt : IBsonSerializationOptions
{
public void ApplyAttribute(IBsonSerializer serializer, Attribute attribute)

{ throw new NotImplementedException(); }

public IBsonSerializationOptions Clone()
{ throw new NotImplementedException(); }

public IBsonSerializationOptions Freeze()

{ throw new NotImplementedException(); }

}
i dont know why code so long and there is a lot of unnessesary types?

i think it would be better to add following method to fetchdbref:
BsonDocument doc = test.FindOne();
var l = doc.Elements.ElementAt(1);
var linkr=l.Value;//dbref there
MongoDBRef refer = linkr.AsDBRef() //
var profit = db.FetchDBRef(refer);

if possible.
thank you.



 Comments   
Comment by Robert Stam [ 21/Mar/13 ]

We wouldn't support mapping a BsonDocument to a MongoDBRef at the BsonDocument (or BsonValue) level because MongoDBRef is a type that is declared outside of the MongoDB.Bson DLL.

Theoretically we could add an overloaded constructor for MongoDBRef that had a BsonDocument as a parameter and it would pick out the $ref and $id from the BsonDocument. But... that's exactly what the serializer does.

Comment by ??????? ??????? ??????? [ 20/Mar/13 ]

var dbRef = new MongoDBRef(document["ref"]["$ref"].AsString, document["ref"]["$id"]);

i consider that this version of cast to mongodbref is redundant because
i think driver can determiny field names "$ref" and "$id" without explicit declaration, isn't it?

 var dbRef = BsonSerializer.Deserialize<MongoDBRef>(document["ref"].AsBsonDocument);

this version is better i like it and will use in extention method

 static class ext
    {
       public static MongoDBRef AsDBRef(this BsonValue val)
       { return BsonSerializer.Deserialize<MongoDBRef>(val.AsBsonDocument); }
    
    }

thanks for hint.

but bsonvalue has a lot of properties As[Type]
and i don't know why the cast to BsonDocument (document["ref"].AsBsonDocument) and
after that deserialize reference is rather than cast to mongodbref itself

my variant of the code is reduced to

var dbRef = doc["ref"].AsDBRef()

yes i made a mistake and your code more compact than my first but
is that code more readable than
var dbRef = BsonSerializer.Deserialize<MongoDBRef>(document["ref"].AsBsonDocument);
?

i think that right option in that situation is

var dbRef = doc["ref"].AsDBRef
//(property)

because it fits driver semantics good and take right place in
property group As[Type]

with the Regards
Dmitry Shevyakov

Comment by Robert Stam [ 05/Mar/13 ]

There is no need to go through the serializer to create an instance of MongoDBRef. For example, if you have the following BsonDocument:

var document = new BsonDocument
{
    { "_id", 1 },
    { "ref", new BsonDocument { { "$ref", "collection" }, { "$id", 2 } } }
};

You can create an instance of MongoDBRef as follows:

var dbRef = new MongoDBRef(document["ref"]["$ref"].AsString, document["ref"]["$id"]);

Which (given the sample document) is essentially doing this:

var dbRef = new MongoDBRef("collection", 2);

And if for some reason you really wanted to use Deserialize, you can do it with one line of code:

var dbRef = BsonSerializer.Deserialize<MongoDBRef>(document["ref"].AsBsonDocument);

Generated at Wed Feb 07 21:37:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.