Beispiel #1
0
        public void GetMongoDBData()
        {
            var mongo = new Mongo();
            mongo.Connect();

            //Get the blog database. If it doesn't exist, that's ok because MongoDB will create it
            //for us when we first use it. Awesome!!!
            var db = mongo.GetDatabase("blog");

            //Get the Post collection. By default, we'll use
            //the name of the class as the collection name. Again,
            //if it doesn't exist, MongoDB will create it when we first use it.
            var collection = db.GetCollection<Post>();

            //this deletes everything out of the collection so we can run this over and over again.
            collection.Remove(p => true);

            //Create a Post to enter into the database.
            var post = new Post()
            {
                Title = "My First Post",
                Body = "This isn't a very long post.",
                CharCount = 27,
                Comments = new List<Comment>
                {
                    { new Comment() { TimePosted = new DateTime(2010,1,1),
                                      Email = "*****@*****.**",
                                      Body = "This article is too short!" } },
                    { new Comment() { TimePosted = new DateTime(2010,1,2),
                                      Email = "*****@*****.**",
                                      Body = "I agree with Bob." } }
                }
            };
            collection.Save(post);
        }
        //    public void testMapReduce(){//插入测试数据
        //Mongo  _db = new Mongo( "127.0.0.1" ).getDB( "jmrtest" );
        //DBCollection c = _db.getCollection( "jmr" );
        //    c.drop();
        //    c.save( new BasicDBObject( "x" , new String[]{ "a" , "b" } ) );
        //    c.save( new BasicDBObject( "x" , new String[]{ "b" , "c" } ) );
        //    c.save( new BasicDBObject( "x" , new String[]{ "c" , "d" } ) );
        //    MapReduceOutput out =
        //        c.mapReduce( "function(){ " +
        //                "for( var i=0; i<this.x.length; i++ )" +
        //                "{ emit(this.x[i] , 1 ); } }",//map函数内使用this来操作当前行表示的对象,并且使用emit(key,value)方法来向reduce提供参数:
        //                "function(key,values){" +
        //                " var sum=0; " +
        //                "for( var i=0; i<values.length; i++ )" +
        //                " sum += values[i];" +
        //                " return sum;}" ,//reduce的key就是emit(key,value)的key,value_array是同个key对应的多个value数组:
        //                     "result" , null );//mapReduce 方法2.x以后和1.X不同
        //    Map<String,Integer> m = new HashMap<String,Integer>();
        //    for (DBObject element :  out.results()) {
        //         System.out.println(element.get("value"));
        //    }
        //    for ( DBObject r : out.results() ){
        //        System.out.println( ((Number)(r.get( "value" ))).intValue());
        //        m.put( r.get( "_id" ).toString() , ((Number)(r.get( "value" ))).intValue() );
        //    }
        //    System.out.println(m.size());
        //    assertEquals( 4 , m.size() );
        //    assertEquals( 1 , m.get( "a" ).intValue() );
        //    assertEquals( 2 , m.get( "b" ).intValue() );
        //    assertEquals( 2 , m.get( "c" ).intValue() );
        //    assertEquals( 1 , m.get( "d" ).intValue() );
        //}
        private static void CreatePosts(IMongoCollection<Post> collection)
        {
            var post = new Post()
            {
                Title = "My First Post",
                Body = "This isn't a very long post.",
                CharCount = 27,
                Comments = new List<Comment>
                {
                    { new Comment() { TimePosted = new DateTime(2010,1,1), Email = "*****@*****.**", Body = "This article is too short!" } },
                    { new Comment() { TimePosted = new DateTime(2010,1,2), Email = "*****@*****.**", Body = "I agree with Bob." } }
                }
            };

            //Save the post.  This will perform an upsert.  As in, if the post already exists, update it, otherwise insert it.
            collection.Save(post);

            //Get the first post that is not matching correctly...
            post = collection.Linq().First(x => x.CharCount != x.Body.Length);

            post.CharCount = post.Body.Length;

            //this will perform an update this time because we have already inserted it.
            collection.Save(post);

            post = new Post()
            {
                Title = "My Second Post",
                Body = "This still isn't a very long post.",
                CharCount = 34,
                Comments = new List<Comment>
                {
                    { new Comment() { TimePosted = new DateTime(2010,1,1), Email = "*****@*****.**", Body = "This isn't any better" } },
                }
            };

            //Save the post.  This will perform an upsert.  As in, if the post already exists, update it, otherwise insert it.
            collection.Save(post);

            post = new Post()
            {
                Title = "My Third Post",
                Body = "Ok, fine.  I'm writing a longer post so that Bob will leave me alone.",
                CharCount = 69,
                Comments = new List<Comment>
                {
                    { new Comment() { TimePosted = new DateTime(2010,1,1), Email = "*****@*****.**", Body = "Yeah, well, you are a terrible blogger." } },
                }
            };

            //Save the post.  This will perform an upsert.  As in, if the post already exists, update it, otherwise insert it.
            collection.Save(post);
        }