当前位置: 首页>>代码示例>>C#>>正文


C# IMongoCollection.Save方法代码示例

本文整理汇总了C#中IMongoCollection.Save方法的典型用法代码示例。如果您正苦于以下问题:C# IMongoCollection.Save方法的具体用法?C# IMongoCollection.Save怎么用?C# IMongoCollection.Save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IMongoCollection的用法示例。


在下文中一共展示了IMongoCollection.Save方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreatePosts

        //    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 = "[email protected]", Body = "This article is too short!" } },
                    { new Comment() { TimePosted = new DateTime(2010,1,2), Email = "[email protected]", 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 = "[email protected]", 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 = "[email protected]", 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);
        }
开发者ID:sridhar19091986,项目名称:pcapsctpspliter,代码行数:85,代码来源:Program.cs

示例2: CreateEmployees

        private static void CreateEmployees(IMongoCollection<Employee> collection)
        {
            var webshop = new Project() { ProjectCode = "P1", ProjectName = "Online Store" };
            var secret = new Project() { ProjectCode = "P2", ProjectName = "Secret Project" };
            var finance = new Project() { ProjectCode = "P3", ProjectName = "Finance System" };

            var emp = new Employee()
            {
                Name = "Fernando",
                PhoneNumber = "1234",
                Projects = new List<Project>
                {
                    { webshop },
                    { secret }
                }
            };

            collection.Save(emp);

            emp = new SalariedEmployee()
            {
                Name = "Felipe",
                PhoneNumber = "5678",
                PayGrade = 7,
                Projects = new List<Project>
                {
                    { secret },
                    { finance }
                }
            };

            collection.Save(emp);

            emp = new Employee()
            {
                Name = "Nico",
                PhoneNumber = "9876",
                Projects = new List<Project>
                {
                    { webshop },
                }
            };

            collection.Save(emp);
        }
开发者ID:jhpaterson,项目名称:MappingExampleMongoDB,代码行数:45,代码来源:Program.cs

示例3: CrearPosts

        //poblamos la BD con unos cuantos posts y comentarios
        private static void CrearPosts(IMongoCollection<Post> collection)
        {
            var post = new Post()
            {
                Titulo = "mi primer post",
                Contenido = "hola amigos soy nuevo en esto",
                Longitud = 27,
                Comentarios = new List<Comentario>
                {
                    { new Comentario() { 
                        FechaPosteo = new DateTime(2014,1,1),
                        Email = "[email protected]",
                        Contenido = "es un post demasiado corto" } },
                    { new Comentario() {
                        FechaPosteo = new DateTime(2014,1,2),
                        Email = "[email protected]",
                        Contenido = "estoy de acuerdo con anónimo" } }
                }
            };

            //guardamos el post. esto realiza un upsert, es decir, si no existe se inserta, caso contrario, se actualiza.
            collection.Save(post);

            //obtenemos el primer post que no corresponda
            post = collection.Linq().First(x => x.Longitud != x.Contenido.Length);

            post.Longitud = post.Contenido.Length;

            //esto resulta en un update ya que ya insertamos el registro
            collection.Save(post);

            post = new Post()
            {
                Titulo = "Mi Segundo Post",
                Contenido = "Este post no es largo",
                Longitud = 34,
                Comentarios = new List<Comentario>
                {
                    { new Comentario() { FechaPosteo = new DateTime(2014,1,1), Email = "[email protected]", Contenido = "Parece que no has mejorad." } },
                }
            };

            //guardamos el post. esto realiza un upsert, es decir, si no existe se inserta, caso contrario, se actualiza.
            collection.Save(post);

            post = new Post()
            {
                Titulo = "Mi tercer post",
                Contenido = "Bueno este post si es mucho más grande así que espero que esten contentos",
                Longitud = 69,
                Comentarios = new List<Comentario>
                {
                    { new Comentario() { FechaPosteo = new DateTime(2014,1,1), Email = "[email protected]", Contenido = "No calificaría a esto como un post." } },
                }
            };

            //guardamos el post. esto realiza un upsert, es decir, si no existe se inserta, caso contrario, se actualiza.
            collection.Save(post);
        }
开发者ID:vash47,项目名称:proyecto-nosql,代码行数:60,代码来源:Program.cs


注:本文中的IMongoCollection.Save方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。