DEV Community

Discussion on: Tutorial: MongoDB With C# The Easy Way!

Collapse
 
catalinradoi profile image
CatalinRadoi

Hi. Shouldn't "new DB()" be assigned to an object or something?

Collapse
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

not necessary if you're only dealing with a single database. if dealing with multiple dbs, then you need to assign to a variable as shown here.

Collapse
 
catalinradoi profile image
CatalinRadoi

Thanks! One more question :P I'm a MongoDB Newbie

I have an entity called Zone, with only one property: public int SqlId { get;set; }

And I have an entity Search:
public class Search : Entity {
public Many Zones { get; set; }
public Search() => this.InitOneToMany(() => this.Zones);
}

this is how an add a new Search:

var search = new Search();
search.Save();
var zone1 = new Zone { SqlId = 1};
zone1.Save();

search.Zones.Add(zone1);

var zone2 = new Zone { SqlId = 2};
zone2.Save();
search.Zones.Add(zone2);

search.Save();

I have multiple issues:

  1. Zones are duplicated in MongoDb. A new document is created for each new SqlId.

I wanted to use the Id from Mongo, but I can't "overwrite" that one. Then I need to make this SqlId unique or something.

  1. There seem to be a lot of "saves" is this good (performance-wise)?
Thread Thread
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

you can do bulk saves like this:

            var zones = new[] {
                new Zone { SqlId = 1 },
                new Zone { SqlId = 2 }
            };

            DB.Save(zones);

            var search = new Search();
            search.Save();

            search.Zones.Add(zones);

Zones are duplicated in MongoDb. A new document is created for each new SqlId.

i don't really understand your requirement here. do you not want to create two new zone documents?

Thread Thread
 
catalinradoi profile image
CatalinRadoi

Thanks for answering, you are awesome :)

no, I don't want to create two new zones.
Zones are like a META table or "Dictionary".

I only have 20 zones, from id = 1 to id = 20

A search could have a combination of these zones.
1, 2
or 1,2,3

I want to create two new Search documents, but I don't want the Zones to be created each time.

Thread Thread
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

for seeding the zone entities once, you can use the migrations feature by first writing a migration class like this:

public class _001_seed_zone_data : IMigration
{
    public void Upgrade()
    {
        var zones = new List<Zone>();

        for (int i = 1; i <= 20; i++)
        {
            zones.Add(new Zone { SqlId = i });
        }

        zones.Save();
    }
}

then on your application startup simply call:

    DB.Migrate();

then you can create your searches like this:

    var wantedZones = new[] { 1, 2, 3 };

    var foundZones = DB.Find<Zone>()
                       .Many(z => wantedZones.Contains(z.SqlId));

    var seach1Zones = foundZones.Where(z => new[] { 1, 2 }.Contains(z.SqlId));
    var search2Zones = foundZones.Where(z => new[] { 1, 2, 3 }.Contains(z.SqlId));

    var search1 = new Search();
    var search2 = new Search();

    DB.Save(new[] { search1, search2 });

    search1.Zones.Add(seach1Zones);
    search2.Zones.Add(search2Zones);
Thread Thread
 
catalinradoi profile image
CatalinRadoi

Thanks, you are very cool. I will try it now

Thread Thread
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

here's my test program in case you need to have a look: gist.github.com/dj-nitehawk/8bde0d...

Thread Thread
 
catalinradoi profile image
CatalinRadoi

Thank you :) I bought you a coffee for your outstanding help.

This project has a lot of potentials! I will recommend it to fellow C# devs.

Thread Thread
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

you are most welcome. just received the email notification regarding the donation. much appreciated. if you ever need any more help, just go to the github repo and open up a new issue. i'll be more than glad to help. have a great day!