.NET best practices for MongoDB connections?

前端 未结 6 968
臣服心动
臣服心动 2020-12-12 14:40

I\'ve been playing with MongoDB recently (It\'s AMAZINGLY FAST) using the C# driver on GitHub. Everything is working just fine in my little single threaded console app that

6条回答
  •  悲&欢浪女
    2020-12-12 14:50

    Somewhat but still of interest is CSMongo, a C# driver for MongoDB created by the developer of jLinq. Here's a sample:

    //create a database instance
    using (MongoDatabase database = new MongoDatabase(connectionString)) {
    
        //create a new document to add
        MongoDocument document = new MongoDocument(new {
            name = "Hugo",
            age = 30,
            admin = false
        });
    
        //create entire objects with anonymous types
        document += new {
            admin = true,
            website = "http://www.hugoware.net",
            settings = new {
                color = "orange",
                highlight = "yellow",
                background = "abstract.jpg"
            }
        };
    
        //remove fields entirely
        document -= "languages";
        document -= new[] { "website", "settings.highlight" };
    
        //or even attach other documents
        MongoDocument stuff = new MongoDocument(new {
            computers = new [] { 
                "Dell XPS", 
                "Sony VAIO", 
                "Macbook Pro" 
                }
            });
        document += stuff;
    
        //insert the document immediately
        database.Insert("users", document);
    
    }
    

提交回复
热议问题