本文整理汇总了C#中IMongoCollection.Linq方法的典型用法代码示例。如果您正苦于以下问题:C# IMongoCollection.Linq方法的具体用法?C# IMongoCollection.Linq怎么用?C# IMongoCollection.Linq使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IMongoCollection
的用法示例。
在下文中一共展示了IMongoCollection.Linq方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
示例2: 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);
}