MongoDBѧϰ±Ê¼Ç(ËÄ) ÓÃMongoDBµÄÎĵµ½á¹¹ÃèÊöÊý¾Ý¹Øϵ
¡¡¡¡MongoDBµÄ¼¯ºÏ£¨collection£©¿ÉÒÔ¿´×ö¹ØϵÐÍÊý¾Ý¿âµÄ±í£¬Îĵµ¶ÔÏó£¨document£©¿ÉÒÔ¿´×ö¹ØϵÐÍÊý¾Ý¿âµÄÒ»Ìõ¼Ç¼¡£µ«Á½ Õß²¢²»ÍêÈ«¶ÔµÈ¡£±íµÄ½á¹¹Êǹ̶¨µÄ£¬MongoDB¼¯ºÏ²¢Ã»ÓÐÕâ¸öÔ¼Êø£»ÁíÍ⣬´æÈ뼯ºÏµÄÎĵµ¶ÔÏóÉõÖÁ¿ÉÒÔǶÈë×ÓÎĵµ£¬»òÕß¡°×Ó¼¯ºÏ¡±¡£ËûÃÇ×îÖÕ¶¼¿ÉÒÔÓà ÀàËÆÓÚBJSONµÄ¸ñʽÃèÊö¡£ÎÒÃǽñÌì¾ÍÀ´·ÖÎöMongoDBÕâÒ»ÌØÐÔ´øÀ´µÄ¶ÀÌØÊý¾Ý¹ÜÀí·½Ê½¡£ÎÒÃÇ»¹ÊÇÒÔsamusÇý¶¯ÎªÀýÀ´·ÖÎö£¬samusÇý¶¯Ö§³Ö Á½ÖÖ·½Ê½·ÃÎÊÊý¾Ý¿â£¬»ù±¾·½Ê½ºÍlinq·½Ê½£¬»ù±¾·½Ê½ÔÚÉÏƪÒÔ½éÉܹý£¬linq·½Ê½ÎÒ²»Ïëµ¥¶À½²½âÓ¦ÓÃʵÀý£¬ÕâƪÎÒ»áÓÃÁ½ÖÖ·½Ê½À´¶Ô±È½éÉÜ¡£
Ò»¡¢°üº¬×ÓÎĵµµÄ¼¯ºÏ²Ù×÷
¡¡¡¡ÓÐÕâôһ¸öÓ¦Óó¡¾°£¬Ä³ÍøÕ¾Ìṩ»áÔ±µÇ¼µÄ¹¦ÄÜ£¬Óû§ÐèҪע²áÕ˺ŲÅÄÜÏíÊÜ»áÔ±·þÎñ£¬µ«ÊÇ×¢²áÕß¿ÉÄÜ»áÒòΪÓû§×ÊÁÏ±íµ¥ÊäÈëÏî¹ý´ó¶ø·ÅÆúÌîд£¬Òò´ËÓû§ÐÅÏ¢·ÖΪÖ÷Òª×ÊÁϺÍÏêϸ×ÊÁÏÁ½Ï³õ´Î×¢²áÖ»ÐèÒªÌîдÖ÷Òª×ÊÁϾÍÐÐÁË¡£ÎÒÃÇ´òËã°ÑÏêϸÐÅÏ¢Éè¼ÆΪ×ÓÎĵµ´æ´¢¡£
¡¡¡¡ 1) linq·½Ê½ÊµÏÖ
¡¡¡¡1. н¨Êý¾ÝÃèÊöÀ࣬ÃèÊöÓû§ÐÅÏ¢
/// <summary> /// Óû§Ö÷Òª×ÊÁÏ /// </summary> public class UserInfo { public string UserId { get; set; } public string UserName { get; set; } public string PassWord { get; set; } public Detail Detail { get; set; } } /// <summary> /// Óû§Ïêϸ×ÊÁÏ /// </summary> public class Detail { public string Address { get; set; } public int Age { get; set; } public string Email { get; set; } }2. ÎÒÃÇҪн¨Ò»¸öÓû§ÒµÎñ²Ù×÷Àà¡°UserBLL¡±¡£Õâ¸öʱºòÒªÈÃÇý¶¯ÖªµÀUserInfoÀàÃèÊöÁË¡°Óû§×ÊÁÏ¡±µÄ×Ö¶ÎÐÅÏ¢£¬ÔÚGetMongo()·½·¨ÊµÏÖÁËÅäÖò½Ö裬UserBLLÍêÕû´úÂëÈçÏ£º
public class UserBLL { public string connectionString = "mongodb://localhost"; public string databaseName = "myDatabase"; private Mongo mongo; private MongoDatabase mongoDatabase; //×¢ÒâÕâÀï·ºÐÍÀàÐÍΪ¡°UserInfo¡± private MongoCollection<UserInfo> mongoCollection; public UserBLL() { mongo = GetMongo(); mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase; mongoCollection = mongoDatabase.GetCollection<UserInfo>() as MongoCollection<UserInfo>; mongo.Connect(); } ~UserBLL() { mongo.Disconnect(); } /// <summary> /// ÅäÖÃMongo,½«ÀàUserInfoÓ³Éäµ½¼¯ºÏ /// </summary> private Mongo GetMongo() { var config = new MongoConfigurationBuilder(); config.Mapping(mapping => { mapping.DefaultProfile(profile => { profile.SubClassesAre(t => t.IsSubclassOf(typeof(UserInfo))); }); mapping.Map<UserInfo>(); }); config.ConnectionString(connectionString); return new Mongo(config.BuildConfiguration()); } }3. ½Ó×Å£¬ÔÚ¡°UserBLL¡±ÀàÖж¨ÒåÒ»¸ö·½·¨¡°InsertSomeData()¡±À´²åÈëһЩÊý¾Ý£º
/// <summary> /// ²åÈëһЩÊý¾Ý /// </summary> public void InsertSomeData() { UserInfo userInfo1 = new UserInfo() { UserId = "1001", UserName = "ÕÅÈý", PassWord = "123456" }; mongoCollection.Save(userInfo1); UserInfo userInfo2 = new UserInfo() { UserId = "1002", UserName = "ÀîËÄ", PassWord = "123456", Detail = new Detail() { Address = "ºþ±±", Age = 20, Email = "lisi@163.com" } }; mongoCollection.Save(userInfo2); UserInfo userInfo3 = new UserInfo() { UserId = "1003", UserName = "ÍõÎå", PassWord = "123456", Detail = new Detail() { Address = "¹ã¶«", Age = 20, Email = "wangwu@163.com" } }; mongoCollection.Save(userInfo3); UserInfo userInfo4 = new UserInfo() { UserId = "1004", UserName = "ÕÔÁù", PassWord = "123456", Detail = new Detail() { Address = "ºþ±±" } }; mongoCollection.Save(userInfo4); }4. ¶¨ÒåÒ»¸ö²éÕÒÊý¾ÝµÄ·½·¨¡°Select¡±£¬Ëü½«²éÕÒÓû§ÏêϸÐÅÏ¢ÖУ¬µØÖ·ÔÚºþ±±µÄÈ«²¿Óû§£º
/// <summary> /// ²éѯÏêϸ×ÊÁϵØַΪºþ±±µÄÓû§ÐÅÏ¢ /// </summary> public List<UserInfo> Select() { return mongoCollection.Linq().Where(x => x.Detail.Address == "ºþ±±").ToList(); }¡¡5. »¹¶¨ÒåÒ»¸öɾ³ýÊý¾ÝµÄ·½·¨£¬½«É¾³ý¼¯ºÏÈ«²¿Êý¾Ý£º
/// <summary> /// ɾ³ýÈ«²¿Óû§ÐÅÏ¢ /// </summary> public void DeleteAll() { mongoCollection.Remove(x => true); }6. ÔÚMain·½·¨ÖÐÌí¼ÓÈçÏ´úÂ룺
static void Main(string[] args) { UserBLL userBll = new UserBLL(); userBll.InsertSomeData(); var users = userBll.Select(); foreach (var user in users) { Console.WriteLine(user.UserName + "ÊǺþ±±ÈË"); }; userBll.DeleteAll(); }7. ×îºóÖ´ÐгÌÐò£¬´òÓ¡ÈçÏÂÐÅÏ¢£º
ÀîËÄÊǺþ±±ÈË |
ÕÔÁùÊǺþ±±ÈË |
¡¡¡¡ 1) ÆÕͨʵÏÖ
¡¡¡¡ÆÕͨ·½Ê½ÊµÏÖ²»Ïë¶à½²£¬Ö±½ÓÌù´úÂ룬¿´¿´Óëlinq·½Ê½ÓÐʲôÇø±ð£º
class Program { static void Main(string[] args) { UserBLL userBll = new UserBLL(); userBll.InsertSomeData(); var users = userBll.Select(); foreach (var user in users) { Console.WriteLine(user["UserName"].ToString() + "ÊǺþ±±ÈË"); }; userBll.DeleteAll(); Console.ReadLine(); } } public class UserBLL { public string connectionString = "mongodb://localhost"; public string databaseName = "myDatabase"; public string collectionName = "UserInfo"; private Mongo mongo; private MongoDatabase mongoDatabase; private MongoCollection<Document> mongoCollection; public UserBLL() { mongo = new Mongo(connectionString); mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase; mongoCollection = mongoDatabase.GetCollection<Document>(collectionName) as MongoCollection<Document>; mongo.Connect(); } ~UserBLL() { mongo.Disconnect(); } /// <summary> /// ²åÈëһЩÊý¾Ý /// </summary> public void InsertSomeData() { Document userInfo1 = new Document(); userInfo1["UserId"] = "1001"; userInfo1["UserName"] = "ÕÅÈý"; userInfo1["PassWord"] = "123456"; mongoCollection.Save(userInfo1); Document userInfo2 = new Document(); userInfo2["UserId"] = "1002"; userInfo2["UserName"] = "ÀîËÄ"; userInfo2["PassWord"] = "123456"; //×ÓÎĵµ var userInfo2Detail = new Document(); userInfo2Detail["Address"] = "ºþ±±"; userInfo2Detail["Age"] = 20; userInfo2Detail["Email"] = "lisi@163.com"; userInfo2["Detail"] = userInfo2Detail; mongoCollection.Save(userInfo2); Document userInfo3 = new Document(); userInfo3["UserId"] = "1003"; userInfo3["UserName"] = "ÍõÎå"; userInfo3["PassWord"] = "123456"; var userInfo3Detail = new Document(); userInfo3Detail["Address"] = "¹ã¶«"; userInfo3Detail["Age"] = 20; userInfo3Detail["Email"] = "wangwu@163.com"; userInfo3["Detail"] = userInfo3Detail; mongoCollection.Save(userInfo3); Document userInfo4 = new Document(); userInfo4["UserId"] = "1004"; userInfo4["UserName"] = "ÕÔÁù"; userInfo4["PassWord"] = "123456"; var userInfo4Detail = new Document(); userInfo4Detail["Address"] = "ºþ±±"; userInfo4["Detail"] = userInfo4Detail; mongoCollection.Save(userInfo4); } /// <summary> /// ²éѯÏêϸ×ÊÁϵØַΪºþ±±µÄÓû§ÐÅÏ¢ /// </summary> public IEnumerable<Document> Select() { return mongoCollection.Find(new Document { { "Detail.Address", "ºþ±±" } }).Documents; } /// <summary> /// ɾ³ýÈ«²¿Óû§ÐÅÏ¢ /// </summary> public void DeleteAll() { mongoCollection.Remove(new Document { }); } }×îºó£¬ÎÒÃÇͨ¹ýÕâ¶Î´úÂëÊä³öÈ«²¿Óû§×ÊÁÏÐÅÏ¢µÄBJSON¸ñʽ£º
/// <summary> /// ´òÓ¡Êý¾ÝBJSON /// </summary> public void PrintBJSON() { string BJSON = string.Empty; foreach (var documet in mongoCollection.FindAll().Documents) { BJSON += documet.ToString(); } Console.WriteLine(BJSON); }½á¹ûÈçÏ£º
{ "UserId": "1001", "UserName": "ÕÅÈý", "PassWord": "123456", "_id": "4d80ec1ab8a4731338000001" } { "UserId": "1002", "UserName": "ÀîËÄ", "PassWord": "123456", "Detail": { "Address": "ºþ±±", "Age": 20, "Email": "lisi@163.com" }, "_id": "4d80ec1ab8a4731338000002" } { "UserId": "1003", "UserName": "ÍõÎå", "PassWord": "123456", "Detail": { "Address": "¹ã¶«", "Age": 20, "Email": "wangwu@163.com" }, "_id": "4d80ec1ab8a4731338000003" } { "UserId": "1004", "UserName": "ÕÔÁù", "PassWord": "123456", "Detail": { "Address": "ºþ±±" }, "_id": "4d80ec1ab8a4731338000004" }
¶þ¡¢°üº¬¡°×Ó¼¯ºÏ¡±µÄ¼¯ºÏ²Ù×÷
¡¡¡¡Í¬Ñù¾Ù¸öÀý×Ó£ºÓÐÒ»¸öѧУÈËʹÜÀíϵͳҪͳ¼Æ°à¼¶ºÍѧÉúµÄÐÅÏ¢£¬ÏÖÔÚ¶¨ÒåÁËÒ»¸ö¡°°à¼¶¼¯ºÏ¡±£¬Õâ¸ö¼¯ºÏÀïÃæµÄѧÉú×Ö¶ÎÊÇÒ»¸ö¡°Ñ§Éú¼¯ºÏ¡±£¬°üº¬Á˱¾°àÈ«²¿Ñ§Éú¡£
¡¡¡¡ 1) linq·½Ê½ÊµÏÖ
¡¡¡¡»ù´¡ÅäÖÃÎҾͲ»¶à˵ÁË£¬Êý¾ÝÀඨÒåÈçÏ£º
/// <summary> /// °à¼¶ÐÅÏ¢ /// </summary> public class ClassInfo { public string ClassName { get; set; } public List<Student> Students { get; set; } } /// <summary> /// ѧÉúÐÅÏ¢ /// </summary> public class Student { public string Name { get; set; } public int Age { get; set; } }²éѯ½Ð¡°ÕÅÈý¡±µÄѧÉúÔÚÄĸö°à¼¶£¬ÒÔ¼°ËûµÄÏêϸÐÅÏ¢£º
(ÕâÀïÆäʵÊÇToListºóÔÚÄÚ´æÖвéµÄ,linq·½Ê½Ö±½Ó²éѯºÃÏñÇý¶¯²»Ö§³Ö¡£)
public List<ClassInfo> Select() { return mongoCollection.Linq().ToList().Where(x => x.Students.Exists(s => s.Name == "ÕÅÈý")).ToList(); }1) ÆÕͨʵÏÖ
¡¡¡¡²éѯ½Ð¡°ÕÅÈý¡±µÄѧÉúÔÚÄĸö°à¼¶£¬ÒÔ¼°ËûµÄÏêϸÐÅÏ¢£º
public List<Document> Select() { var mongocollection = mongoDatabase.GetCollection("ClassInfo"); return mongocollection.Find(new Document { { "Students.Name", "ÕÅÈý" } }).Documents.ToList(); }¡¡´òÓ¡Êý¾ÝµÄBJSON£º
{ "_id": "4d814bae5c5f000000005f63", "ClassName": "1001", "Students": [ { "Name": "ÕÅÈý", "Age": 10 }, { "Name": "ÀîËÄ", "Age": 0 } ] } { "_id": "4d814bae5c5f000000005f64", "ClassName": "1002", "Students": [ ] } { "_id": "4d814bae5c5f000000005f65", "ClassName": "1003", "Students": [ { "Name": "ÍõÎå", "Age": 11 }, { "Name": "ÕÔÁù", "Age": 9 } ] }
Èý¡¢Ð¡½á
¡¡¡¡Í¨¹ý±¾½ÚÀý×ÓÎÒÃÇ·¢ÏÖ£¬MongoDBÓÐËü¶ÀÌصÄÎĵµ½á¹¹¿ÉÒÔÃèÊöÊý¾Ý¶ÔÏóÖ®¼äµÄһЩ¹ØϵÌØÕ÷¡£ËüËäȻûÓйØϵÐÍÊý¾Ý¿â¶à±í·ûºÏ²éѯÄÇÑùÇ¿´óµÄ ±í¼ä²éѯ·½Ê½£¬µ«Ò²¿ÉÒÔͨ¹ýÎĵµ½á¹¹ÃèÊö¸üÁé»îµÄ¹ØϵÌØÐÔ£¬¿ÉÒÔÕâô˵£¬¹ØϵÐÍÊý¾Ý¿âÄÜ×öµÄ£¬MongoDB»ù±¾ÉÏÒ²¿ÉÒÔ×öµ½¡£ÉõÖÁÓÐЩ¹ØϵÊý¾Ý¿â²»ÈÝÒ× ×öµ½µÄ£¬MongoDBÒ²¿ÉÒÔÇáËÉ×öµ½£¬±ÈÈ磬ÃèÊöÊý¾ÝÀàµÄ¼Ì³Ð¹ØϵµÈ¡£
ÔÎĵØÖ·£ºhttp://www.cnblogs.com/lipan/archive/2011/03/17/1986616.html
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ