Permalink
Browse files

Add FindOne(string javascriptWhere)

  • Loading branch information...
1 parent 41d4628 commit 79eebe431649262b6de383b37fb08884e0de8afa @lanwin lanwin committed May 11, 2010
Showing with 33 additions and 15 deletions.
  1. +14 −4 source/MongoDB/IMongoCollection_1.cs
  2. +19 −11 source/MongoDB/MongoCollection_1.cs
@@ -43,6 +43,15 @@ public interface IMongoCollection<T>
/// <summary>
/// Finds and returns the first document in a selector query.
/// </summary>
+ /// <param name="javascriptWhere">The where.</param>
+ /// <returns>
+ /// A <see cref="Document"/> from the collection.
+ /// </returns>
+ T FindOne(string javascriptWhere);
+
+ /// <summary>
+ /// Finds and returns the first document in a selector query.
+ /// </summary>
/// <param name="selector">The selector.</param>
/// <returns>
/// A <see cref="Document"/> from the collection.
@@ -58,11 +67,12 @@ public interface IMongoCollection<T>
ICursor<T> FindAll();
/// <summary>
- /// Uses the $where operator to query the collection. The value of the where is Javascript that will
- /// produce a true for the documents that match the criteria.
+ /// Uses the $where operator to query the collection. The value of the where is Javascript that will
+ /// produce a true for the documents that match the criteria.
/// </summary>
- /// <param name = "where">Javascript</param>
- ICursor<T> Find(string where);
+ /// <param name="javascriptWhere">Javascript</param>
+ /// <returns></returns>
+ ICursor<T> Find(string javascriptWhere);
/// <summary>
/// Queries the collection using the query selector.
@@ -73,21 +73,29 @@ public MongoCollection(MongoConfiguration configuration, Connection connection,
}
/// <summary>
+ /// Finds and returns the first document in a selector query.
+ /// </summary>
+ /// <param name="javascriptWhere">The where.</param>
+ /// <returns>
+ /// A <see cref="Document"/> from the collection.
+ /// </returns>
+ public T FindOne(string javascriptWhere)
+ {
+ var spec = new Document { { "$where", new Code(javascriptWhere) } };
+ using(var cursor = Find(spec, -1, 0, null))
+ return cursor.Documents.FirstOrDefault();
+ }
+
+ /// <summary>
/// Finds and returns the first document in a query.
/// </summary>
/// <param name="spec">A <see cref="Document"/> representing the query.</param>
/// <returns>
/// A <see cref="Document"/> from the collection.
/// </returns>
public T FindOne(object spec){
- var cursor = Find(spec, -1, 0, null);
- foreach (var document in cursor.Documents) {
- cursor.Dispose();
- return document;
- }
- //FIXME Decide if this should throw a not found exception instead of returning null.
- return null;
- //this.Find(spec, -1, 0, null)[0];
+ using(var cursor = Find(spec, -1, 0, null))
+ return cursor.Documents.FirstOrDefault();
}
/// <summary>
@@ -102,10 +110,10 @@ public MongoCollection(MongoConfiguration configuration, Connection connection,
/// <summary>
/// Finds the specified where.
/// </summary>
- /// <param name="where">The where.</param>
+ /// <param name="javascriptWhere">The where.</param>
/// <returns></returns>
- public ICursor<T> Find(string @where){
- var spec = new Document { { "$where", new Code(@where) } };
+ public ICursor<T> Find(string javascriptWhere){
+ var spec = new Document { { "$where", new Code(javascriptWhere) } };
return Find(spec, 0, 0, null);
}

0 comments on commit 79eebe4

Please sign in to comment.