diff --git a/source/MongoDB/IMongoCollection_1.cs b/source/MongoDB/IMongoCollection_1.cs index e5a1b379..77df8f59 100644 --- a/source/MongoDB/IMongoCollection_1.cs +++ b/source/MongoDB/IMongoCollection_1.cs @@ -40,6 +40,15 @@ public interface IMongoCollection /// CollectionMetadata MetaData { get; } + /// + /// Finds and returns the first document in a selector query. + /// + /// The where. + /// + /// A from the collection. + /// + T FindOne(string javascriptWhere); + /// /// Finds and returns the first document in a selector query. /// @@ -58,11 +67,12 @@ public interface IMongoCollection ICursor FindAll(); /// - /// 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. /// - /// Javascript - ICursor Find(string where); + /// Javascript + /// + ICursor Find(string javascriptWhere); /// /// Queries the collection using the query selector. diff --git a/source/MongoDB/MongoCollection_1.cs b/source/MongoDB/MongoCollection_1.cs index e38bb536..6a40a725 100644 --- a/source/MongoDB/MongoCollection_1.cs +++ b/source/MongoDB/MongoCollection_1.cs @@ -72,6 +72,20 @@ public MongoCollection(MongoConfiguration configuration, Connection connection, get { return _metaData ?? (_metaData = new CollectionMetadata(_configuration, DatabaseName, Name, _connection)); } } + /// + /// Finds and returns the first document in a selector query. + /// + /// The where. + /// + /// A from the collection. + /// + 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(); + } + /// /// Finds and returns the first document in a query. /// @@ -80,14 +94,8 @@ public MongoCollection(MongoConfiguration configuration, Connection connection, /// A from the collection. /// 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(); } /// @@ -102,10 +110,10 @@ public MongoCollection(MongoConfiguration configuration, Connection connection, /// /// Finds the specified where. /// - /// The where. + /// The where. /// - public ICursor Find(string @where){ - var spec = new Document { { "$where", new Code(@where) } }; + public ICursor Find(string javascriptWhere){ + var spec = new Document { { "$where", new Code(javascriptWhere) } }; return Find(spec, 0, 0, null); }