本文整理汇总了C#中IMongoCollection.FindOne方法的典型用法代码示例。如果您正苦于以下问题:C# IMongoCollection.FindOne方法的具体用法?C# IMongoCollection.FindOne怎么用?C# IMongoCollection.FindOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IMongoCollection
的用法示例。
在下文中一共展示了IMongoCollection.FindOne方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UserFavorite
public UserFavorite(Database db, string username)
{
DB = db;
UserFavoriteCollection = DB["UserFavorite"];
// initial from db
var spec = new Document() {
{ "username", username }
};
InternalDocument = UserFavoriteCollection.FindOne(spec);
if (InternalDocument == null)
{
InternalDocument = new Document { { "username", username } };
DB["UserFavorite"].Insert(InternalDocument);
}
}
示例2: UpdateUsingAtomicIncrement
/// <summary>
/// Demoes Updates a single field using an atomic Increment ($inc) operator.
/// </summary>
/// <param name="orders">The orders.</param>
private static void UpdateUsingAtomicIncrement(IMongoCollection orders)
{
Console.WriteLine("\n\n======= Update Document using Increment =======");
var selector = new Document { { "CustomerName", "Daffy Duck" } };
Console.WriteLine("Before Update: " + orders.FindOne(selector));
// Add 2000 to order amount on document matching selector.
orders.Update( new Document {{"$inc", new Document {{"OrderAmount", 2000}} }}, selector );
Console.WriteLine("After Update: " + orders.FindOne(selector));
}
示例3: UpdateDocument
/// <summary>
/// Demo of Updating a document.
/// </summary>
/// <param name="orders">The orders.</param>
private static void UpdateDocument( IMongoCollection orders )
{
Console.WriteLine( "\n\n======= Update Documents =======" );
var selector = new Document {{"CustomerName", "Daffy Duck"}};
Document docToUpdate = orders.FindOne( selector );
Console.WriteLine( "Before Update: " + docToUpdate );
// I'm in the money!
docToUpdate["OrderAmount"] = 1000000.00;
// Update Daffy's account before Hasaan finds him.
orders.Update( docToUpdate );
Console.WriteLine( "After Update: " + orders.FindOne( selector ) );
}
示例4: QueryFromCollection
/// <summary>
/// Demos querying the collection.
/// </summary>
/// <param name="orders">The orders.</param>
private static void QueryFromCollection( IMongoCollection orders )
{
Console.WriteLine( "\n\n======= Query One Document =======" );
// Create a specification to query the orders collection.
var spec = new Document();
spec["CustomerName"] = "Elmer Fudd";
// Run the query.
Document result = orders.FindOne( spec );
Console.WriteLine( string.Format( "Found: {0}", result ) );
}