3

Any ideas on how to represent the following with .net objects?

{
    _id: ’T4Y...AC’, // base64-encoded ObjectId
    name: ’Rick’,
    profile: { ... age, location, interests, etc. ... },
    followers: {
    "T4Y...AD": { name: ’Jared’, circles: [ ’python’, ’authors’] },
    "T4Y...AF": { name: ’Bernie’, circles: [ ’python’ ] },
    "T4Y...AI": { name: ’Meghan’, circles: [ ’python’, ’speakers’ ] }
    }
}
6
  • If the Mongo "introduction" documents were a little better, people wouldn't have to ask like this. mongodb.org/display/DOCS/CSharp+Driver+Tutorial is quite lacking when it comes to automatic serialization. Commented May 30, 2012 at 4:16
  • And yes, I know, MongoDB uses JSON primarily as a way to show data from and store data to the database, and that document appears to give pretty good example of MongoDB's basic types, but generally I'm not working on JSON from scratch. Commented May 30, 2012 at 4:19
  • (the OPs question isn't even using strict JSON... is this even valid in MongoDB? I don't even know.) Commented May 30, 2012 at 4:32
  • 1
    @JayC: mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial. What's missing from this? Commented May 30, 2012 at 13:08
  • @Game99 why do you want to base64 encode the object id? You can apply a BsonRepresentation(BsonType.String) attribute on it and it will work just fine. Commented May 30, 2012 at 13:11

1 Answer 1

1

I would imagine it might look something like

[DataContract]
public class data
{
    [BsonId]
    [DataMember(Order = 0]
    public BsonObjectId { get; set; }

    [DataMember(Order = 1]
    public string name { get; set; }

    [DataMember(Order = 2]
    public Profile profile { get; set; }

    [DataMember(Order = 3]
    public Dictionary<string,Follower> followers { get; set;}
}

[DataContract]
public class Profile
{
    [DataMember(Order = 0]
    public int age { get; set; }
    [DataMember(Order = 1]
    public string location { get ;set; }
    [DataMember(Order = 2]
    public string interests { get ;set; }
}

[DataContract]
public class Follower
{

     [DataMember(Order = 0]
    public string name { get; set; }
    [DataMember(Order = 1]
    public string[] circles
}

I believe both Mongo and WCF support dictionaries so yes you could change Followers to a dictionary

Sign up to request clarification or add additional context in comments.

5 Comments

I'm figuring "followers" actually translates to a dictionary type, but seeing this apparently is using WCF "Serializable" notation, I'm not sure how that translates off the top of my head. Last time I tried to deal with dictionaries in WCF it was a bit wonky, though doable.
You might want to edit your question to include that its working with WCF. I will update my answer
All the DataContract classes can go away and the DataMembers can become BsonElement() -> if you want to change the name of an element, you can include it in the ctor of the BsonElement attribute.
I have had troubles with MongoDB serializing dictionaries in the right way.
@CraigWilson I had tried this and the BsonElement Tag it still didnt serialize. (I want to use camel case in my db json and pascal case in my c# code) Have you seen this method work for serialization?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.