开发者

Architecture around mongodb with document and subdocument

I have an application which manage C# settings. So I have a document which looks like:

{
    ApplicationName: "FooApplication",
    Settings : [
          ConnectionString: "localhost@myserver.com",
          UserName: "Admin"
          Password: "Password"
        ]
    }
}
{
    ApplicationName: "BarApplication",
    Settings : [
          ConnectionString: "localhost@myserver.com",
          UserName: "Admin",
          Password: "Password",
          Special: "aaaaaaaaaaaaaa"
        ]
    }
}

As you can see both of my application FooApplication and BarApplication use the same Settings except that my BarApplication have one more settings which is Special: "aaaaaaa".

The day, my ConnectionString become NewLocalhost@myserver.com, is it the good way to make an update to every Application documents's ConnectionString field ? Actually I don't know what is the best practices in my case as I'm new in using a NoSQL solution like mongodb.

And second thing, I want to insert a new application, I want to always insert some settings like ConnectionString, UserName and Password because its the same for every application but where do I get this common settings ? With a new collection called GlobalSettings where I store the common settings ? or for exemple, do I select the applicationName FooApplication, take the value of his common fields, then copy it to my new application S开发者_运维技巧uperApplication I want to create ?

Any advice would be appreciate.

Thanks guys.


First of all, best practices for most NoSql stores are similar to those of RDBMS stores.

Second of all, lets clarify the terminology.

  • A collection is just what it sounds like - a collection of documents. It is analogous to a table.
  • A document is essentially a record in a collection.

What you most likely want is a collection of settings with a bunch of settings documents, not a single document that contains all of your settings.

You should create a POCO that contains your properties

public class ApplicationSettings {
    public ObjectId Id {get; set;}
    public string Name {get; set;}
    public Dictionary<string, string> Settings {get; set;}
}

Now you can create an instance of ApplicationSettings that can be inserted into the collection.

You can query by id (for which mongo already has an index on by default) or by application name.

When you want to do an update, you can update a single document by querying for it by id or name or you can update all your documents at once.

Assuming you are using the official C# driver, it would look something like this:

Update single document

var query = Query.EQ("_id", someIdHere);
var update = Update.Set("Settings.ConnectionString", "NewLocalhost@myserver.com")
collection.Update(query, update)

(Where you get your collection instance from is up to you.)

Check out the documentation for more info: http://www.mongodb.org/display/DOCS/CSharp+Language+Center

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜