Permalink
Browse files

Added better null support.

Added convenience overload Collection.Find(str) to create $where document th
  • Loading branch information...
1 parent 128b075 commit 0dbf7f26cea95a26aa421edde0620ab5afb2ed84 @samus committed Oct 26, 2009
View
5 .gitignore
@@ -15,7 +15,12 @@
test-results/*.xml
MongoDB.Net-Tests/test-results/MongoDB.Driver.Tests.csproj.test-cache
+MongoDB.Linq.Tests/test-results/MongoDB.Linq.Tests.csproj.test-cache
*.suo
/_UpgradeReport_Files/*
obj/*
+MongoDB.Linq/obj/*
+MongoDB.Linq.Tests/bin/*
+MongoDB.Linq.Tests/obj/*
+
View
12 MongoDB.Net-Tests/Bson/TestBsonConvert.cs
@@ -56,5 +56,17 @@ public class TestBsonConvert
}
+ [Test]
+ public void TestFromNullValueObject(){
+ Object obj = null;
+ BsonType t = BsonConvert.From(obj);
+ Assert.AreEqual(typeof(BsonNull),t.GetType());
+ }
+
+ [Test]
+ public void TestFromMongoDBNull(){
+ BsonType t = BsonConvert.From(MongoDBNull.Value);
+ Assert.AreEqual(typeof(BsonNull),t.GetType());
+ }
}
}
View
28 MongoDB.Net-Tests/TestCollection.cs
@@ -29,6 +29,13 @@ public class TestCollection
}
[Test]
+ public void TestFindNulls(){
+ Document query = new Document().Append("n",MongoDBNull.Value);
+ long numnulls = db["tests"]["smallreads"].Count(query);
+ Assert.AreEqual(4,numnulls);
+ }
+
+ [Test]
public void TestFindAttributeLimit(){
Document query = new Document();
query["j"] = 10;
@@ -57,6 +64,27 @@ public class TestCollection
}
[Test]
+ public void TestManualWhere(){
+ Document query = new Document().Append("$where", new Code("this.j % 2 == 0"));
+ ICursor c = db["tests"]["reads"].Find(query);
+ foreach(Document result in c.Documents){
+ Assert.IsNotNull(result);
+ Object j = result["j"];
+ Assert.IsTrue((double)j % 2 == 0);
+ }
+ }
+
+ [Test]
+ public void TestWhere(){
+ ICursor c = db["tests"]["reads"].Find("this.j % 2 == 0");
+ foreach(Document result in c.Documents){
+ Assert.IsNotNull(result);
+ Object j = result["j"];
+ Assert.IsTrue((double)j % 2 == 0);
+ }
+ }
+
+ [Test]
public void TestFindOneObjectContainingUKPound(){
Document query = new Document();
Document result = db["tests"]["charreads"].FindOne(query);
View
3 MongoDB.Net-Tests/TestCursor.cs
@@ -32,8 +32,9 @@ public void TestCanReadSmall()
reads++;
}
Assert.IsTrue(reads > 0, "No documents were returned.");
- Assert.AreEqual(4, reads, "More than 4 documents in the small reads dataset");
+ Assert.AreEqual(5, reads, "More than 5 documents in the small reads dataset");
}
+
[Test]
public void TestCanReadMore(){
ICursor c = db["tests"]["reads"].FindAll();
View
1 MongoDB.Net-Tests/test-data/tests.smallreads.txt
@@ -2,3 +2,4 @@
{ "_id" : "4a6a1f7f58760000000028ee", "x" : 4, "j" : 2 }
{ "_id" : "4a6a1f7f58760000000028ef", "x" : 4, "j" : 3 }
{ "_id" : "4a6a1f7f58760000000028f0", "x" : 4, "j" : 4 }
+{ "_id" : "4ae6157d982c000000000c39", "x" : 4, "j" : 5, "n" : 1}
View
8 MongoDBDriver/Bson/BsonConvert.cs
@@ -15,6 +15,8 @@ public static class BsonConvert
{
public static BsonType From(Object val){
+ if(val == null) return From(MongoDBNull.Value);
+
Type t = val.GetType();
//special case enums
if(val is Enum){
@@ -48,6 +50,8 @@ public static class BsonConvert
ret = From((Code)val);
}else if(t == typeof(CodeWScope)){
ret = From((CodeWScope)val);
+ }else if(t == typeof(MongoDBNull)){
+ ret = From((MongoDBNull)val);
}else{
throw new ArgumentOutOfRangeException(String.Format("Type: {0} not recognized",t.FullName));
}
@@ -110,6 +114,10 @@ public static class BsonConvert
return new BsonCodeWScope(val);
}
+ public static BsonNull From(MongoDBNull val){
+ return new BsonNull();
+ }
+
public static BsonType Create(BsonDataType type){
BsonType ret = null;
if(type == BsonDataType.Number){
View
6 MongoDBDriver/Bson/BsonNullUndefined.cs
@@ -1,5 +1,7 @@
using System;
+using MongoDB.Driver;
+
namespace MongoDB.Driver.Bson
{
/// <summary>
@@ -19,8 +21,6 @@ public abstract class BsonEmpty : BsonType
get;
}
-
-
public int Read(BsonReader reader){
return 0;
}
@@ -30,7 +30,7 @@ public abstract class BsonEmpty : BsonType
}
public object ToNative(){
- return null;
+ return MongoDBNull.Value;
}
}
View
6 MongoDBDriver/Collection.cs
@@ -59,6 +59,12 @@ public Collection(string name, Connection conn, string dbName)
return this.Find(spec, 0, 0, null);
}
+ public ICursor Find(String where){
+ Document spec = new Document();
+ spec.Append("$where", new Code(where));
+ return this.Find(spec, 0, 0, null);
+ }
+
public ICursor Find(Document spec) {
return this.Find(spec, 0, 0, null);
}
View
2 MongoDBDriver/Database.cs
@@ -36,7 +36,7 @@ public class Database
public List<String> GetCollectionNames(){
IMongoCollection namespaces = this["system.namespaces"];
- ICursor cursor = namespaces.Find(null);
+ ICursor cursor = namespaces.Find(new Document());
List<String> names = new List<string>();
foreach (Document doc in cursor.Documents){
names.Add((String)doc["name"]); //Fix Me: Should filter built-ins
View
1 MongoDBDriver/IMongoCollection.cs
@@ -10,6 +10,7 @@ public interface IMongoCollection {
CollectionMetaData MetaData { get; }
Document FindOne(Document spec);
ICursor FindAll();
+ ICursor Find(String where);
ICursor Find(Document spec);
ICursor Find(Document spec, int limit, int skip);
ICursor Find(Document spec, int limit, int skip, Document fields);
View
1 MongoDBDriver/MongoDB.Driver.csproj
@@ -106,6 +106,7 @@
<Compile Include="Bson\BsonReader.cs" />
<Compile Include="Bson\BsonElement.cs" />
<Compile Include="Cursor.cs" />
+ <Compile Include="MongoDBNull.cs" />
<Compile Include="MongoExceptions.cs" />
<Compile Include="Oid.cs" />
<Compile Include="IO\DeleteMessage.cs" />
View
19 MongoDBDriver/MongoDBNull.cs
@@ -0,0 +1,19 @@
+
+using System;
+
+namespace MongoDB.Driver{
+ /// <summary>
+ /// Placeholder type for database nulls.
+ /// </summary>
+ public class MongoDBNull{
+ static MongoDBNull val = new MongoDBNull();
+ public static MongoDBNull Value {
+ get {
+ return (val);
+ }
+ }
+ protected MongoDBNull(){
+
+ }
+ }
+}

0 comments on commit 0dbf7f2

Please sign in to comment.