MongoDb در سی شارپ (بخش اول)
نویسنده: علی یگانه مقدم
تاریخ: ۱۳۹۵/۱۲/۰۷ ۲۰:۲۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
Install-Package mongocsharpdriver
public class Author
{
public ObjectId Id { get; set; }
public string Name { get; set; }
} public class Language
{
public ObjectId Id { get; set; }
public string Name { get; set; }
} public class Book
{
public ObjectId Id { get; set; }
public string Title { get; set; }
public string ISBN { get; set; }
public int Price { get; set; }
public List<Author> Authors { get; set; }
public Language Language { get; set; }
} var book =new Book()
{
Title = "Gone With Wind",
ISBN = "43442424",
Price = 50000,
Language = new Language()
{
Name = "Persian"
},
Authors = new List<Author>()
{
new Author()
{
Name = "Margaret Mitchell"
},
new Author()
{
Name = "Ali Mahboobi (Translator)"
},
}
}; var client = new MongoClient();
string connectionString = "mongodb://localhost:27017";
MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
var client = new MongoClient(settings); var db = client.GetDatabase("publisher"); var collection = db.GetCollection<Book>("books"); collection.InsertOneAsync(book);
فعلا موجودیتهای مؤلفان و زبان به دلیل اینکه سند اختصاصی برای خود ندارند، با صفر پر شدهاند؛ ولی شناسه یکتای سند، مقدار خود را گرفته است.
عملیات خواندن
var client = new MongoClient();
var db = client.GetDatabase("publisher");
db.DropCollection("books");
var collection = db.GetCollection<Book>("books");
var book =new Book()
{
Title = "Gone With Wind",
ISBN = "43442424",
Price = 50000,
Year = 1936,
LastStock = DateTime.Now.AddDays(-13),
Language = new Language()
{
Name = "Persian"
},
Authors = new List<Author>()
{
new Author()
{
Name = "Margaret Mitchell"
},
new Author()
{
Name = "Ali Mahboobi (Translator)"
},
}
};
var book2 = new Book()
{
Title = "Jane Eyre",
ISBN = "87897897",
Price = 60000,
Year = 1847,
LastStock = DateTime.Now.AddDays(-5),
Language = new Language()
{
Name = "English"
},
Authors = new List<Author>()
{
new Author()
{
Name = "Charlotte Brontë"
},
}
};
var book3 = new Book()
{
Title = "White Fang",
ISBN = "43442424",
Price = 50000,
Year = 1936,
LastStock = DateTime.Now.AddDays(-13),
Language = new Language()
{
Name = "English"
},
Authors = new List<Author>()
{
new Author()
{
Name = "Jack London"
},
new Author()
{
Name = "Philippe Mignon"
},
}
};
var book4 = new Book()
{
Title = "The Lost Symbol",
ISBN = "43442424",
Price = 3500000,
Year = 2009,
LastStock = DateTime.Now.AddDays(-17),
Language = new Language()
{
Name = "Persian"
},
Authors = new List<Author>()
{
new Author()
{
Name = "Dan Brown"
},
new Author()
{
Name = "Mehrdad"
},
}
};
var book7 = new Book()
{
Title = "The Lost Symbol",
ISBN = "43442424",
Price = 47000000,
Year = 2009,
LastStock = DateTime.Now.AddDays(-56),
Language = new Language()
{
Name = "Persian"
},
Authors = new List<Author>()
{
new Author()
{
Name = "Dan Brown"
},
new Author()
{
Name = "Mehrdad"
},
}
};
var book5= new Book()
{
Title = "The Help",
ISBN = "45345e3er3",
Price = 9000000,
Year = 2009,
LastStock = DateTime.Now.AddDays(-2),
Language = new Language()
{
Name = "Enlish"
},
Authors = new List<Author>()
{
new Author()
{
Name = "Kathryn Stockett"
},
}
};
var book6 = new Book()
{
Title = "City of Glass",
ISBN = "454534545",
Price = 500000,
Year = 2009,
LastStock = DateTime.Now,
Language = new Language()
{
Name = "Persian"
},
Authors = new List<Author>()
{
new Author()
{
Name = "Cassandra Clare"
},
new Author()
{
Name = "Ali"
},
}
};
var books = new List<Book> {book, book2, book3, book4, book5, book6,book7};
collection.InsertManyAsync(books); var client = new MongoClient();
var db = client.GetDatabase("publisher");
var collection = db.GetCollection<Book>("books");
var filter=new BsonDocument();
var docs = collection.Find(filter).ToList();
foreach (var book in docs)
{
Console.WriteLine(book.Title + " By "+ book.Authors[0].Name);
} Gone With Wind By Margaret Mitchell Jane Eyre By Charlotte Brontë White Fang By Jack London The Lost Symbol By Dan Brown The Help By Kathryn Stockett City of Glass By Cassandra Clare The Lost Symbol By Dan Brown
var filter = Builders<Book>.Filter.Eq("Year", 2009);
var docs = collection.Find(filter).ToList();
foreach (var book in docs)
{
Console.WriteLine(book.Title + " By "+ book.Authors[0].Name);
} The Lost Symbol By Dan Brown The Help By Kathryn Stockett City of Glass By Cassandra Clare The Lost Symbol By Dan Brown
// var filter=new BsonDocument();
var filterBuilder = Builders<Book>.Filter;
var filter= filterBuilder.Eq("Year", 2009) | filterBuilder.Gte("Price",700000);
var docs = collection.Find(filter).ToList();
foreach (var book in docs)
{
Console.WriteLine(book.Title + " By "+ book.Authors[0].Name);
} Gone With Wind By Margaret Mitchell White Fang By Jack London The Lost Symbol By Dan Brown The Help By Kathryn Stockett City of Glass By Cassandra Clare The Lost Symbol By Dan Brown
var docs = collection.AsQueryable().Where(x => x.Year == 2009 || x.Price <= 50000).ToList();
var sort = Builders<Book>.Sort.Ascending("Title").Descending("Price");
var docs = collection.Find(filter).Sort(sort).ToList();
foreach (var book in docs)
{
Console.WriteLine(book.Title + " By "+ book.Authors[0].Name);
} City of Glass By Cassandra Clare Gone With Wind By Margaret Mitchell The Help By Kathryn Stockett The Lost Symbol By Dan Brown The Lost Symbol By Dan Brown White Fang By Jack London
dotnet add package MongoDB.Driver
public class Book
{
[BsonId]
[BsonRepresentation((BsonType.ObjectId))]
public string Id { get; set; }
}