Skip to content

Commit

Permalink
Add FindOne(string javascriptWhere)
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 11, 2010
1 parent 41d4628 commit 79eebe4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
18 changes: 14 additions & 4 deletions source/MongoDB/IMongoCollection_1.cs
Expand Up @@ -40,6 +40,15 @@ public interface IMongoCollection<T>
/// </summary>
CollectionMetadata MetaData { get; }

/// <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>
Expand All @@ -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.
Expand Down
30 changes: 19 additions & 11 deletions source/MongoDB/MongoCollection_1.cs
Expand Up @@ -72,6 +72,20 @@ public MongoCollection(MongoConfiguration configuration, Connection connection,
get { return _metaData ?? (_metaData = new CollectionMetadata(_configuration, DatabaseName, Name, _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>
Expand All @@ -80,14 +94,8 @@ public MongoCollection(MongoConfiguration configuration, Connection connection,
/// 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>
Expand All @@ -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);
}

Expand Down

0 comments on commit 79eebe4

Please sign in to comment.