2

I'm trying to model subcollections through C# driver, but I'm finding it difficult to do so; could you some help me to do it or some full fledged example for the same please?

I'm trying to acheive this;

{
id:"id", name: 'name', Tokens:[{name:"yes",expiry:'Today'}, {name:"Hello", expiry:"tomorow"}]
}

I have modelled a class like this

Class sample
{
    [BSON]
    public string id{get; set;}
    public string name{get; set;}
    public TokensCollection[] tokens(get; set;} 
} 

public class TokensCollection
{
    public string name{get;set;}
    public string expiry{get;set;}
}

And in the repository I'm trying to initialize the same like this,

Sample sample1 = new Sample{
id = ObjectId.GenerateNewId().ToString();
name = "name";
//Best way to model this? any pointers?
for (int index =1; index <=2; index++)
{
    tokens[index].name = "me";
    tokens[index].expiry = "Today"
}

collection.insert(sample1);

Could someone help me with this?

2 Answers 2

1

I originally answered your question on the MongoDb CSharp Google Group and here's the example for anyone with a similar problem;

using System;
using System.Collections.Generic;
using System.Linq;

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

using MongoDB.Driver;
using MongoDB.Driver.Linq;

namespace Test.ConsoleApp
{

public class Sample
{

    [BsonId]
    public ObjectId Id { get; private set; }
    public string Name { get; set; }
    public List<Token> Tokens { get; set; }

    public Sample()
    {
        Id = ObjectId.GenerateNewId();
        Tokens = new List<Token>();
    }

}

public class Token
{
    public string Name { get; set; }
    public string Expiry { get; set; }
}


public class Program
{
    static void Main(string[] args)
    {
        var server = MongoServer.Create("mongodb://localhost/database?safe=true");
        var database = server.GetDatabase("test");
        var samplesCollection = database.GetCollection<Sample>("samples");

        Console.WriteLine("Creating Sample #1 ... ");

        var sample1 = new Sample();
        sample1.Name = "Sample #1";
        sample1.Tokens.Add(new Token() { Name = "Name #1", Expiry = "Today" });

        Console.WriteLine("Creating Sample #2 ... ");

        var sample2 = new Sample();
        sample2.Name = "Sample #2";
        sample2.Tokens.Add(new Token() { Name = "Name #2", Expiry = "Tomorrow" });
        sample2.Tokens.Add(new Token() { Name = "Name #3", Expiry = "Next Tuesday" });

        Console.WriteLine("Saving Sample #1 and #2 ... ");

        samplesCollection.Save(sample1);
        samplesCollection.Save(sample2);

        Console.WriteLine("Fetching Sample #1 and #2 ... ");

        var sampleOneFromDb = samplesCollection.AsQueryable<Sample>().Where(c => c.Name.Contains("Sample #1"));

        Console.WriteLine("Sample #1 From DB - {0}", sampleOneFromDb.ToJson());
        Console.ReadLine();

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

Comments

0

Well, I managed to find the answer. I used the IList rather using Array and intialised the object like this,

there are couple of changes, in the sample class

   Class Sample
    {
.....

public List<TokensCollection> tokens{get; set;}
}

and initialized the same using

Sample sample1 = new Sample()
{

.....

tokens = new List<TokensCollection>{
new TokensCollection(){name='name1', expiry ='expiry1'},
new TokensCollection(){name='name2', expiry ='expiry2'}
}

}

It should insert your Embedded Collection document like a charm. If you want to add more docments you should use

tokes.add(new TokensCollection(){name='name3', expiry='expiry3})

all the best

Comments

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.