Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Added better null support.
Browse files Browse the repository at this point in the history
Added convenience overload Collection.Find(str) to create $where document th
  • Loading branch information
samus committed Oct 26, 2009
1 parent 128b075 commit 0dbf7f2
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/*

12 changes: 12 additions & 0 deletions MongoDB.Net-Tests/Bson/TestBsonConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,17 @@ public void TestDBRefRoundTrip(){

}

[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());
}
}
}
28 changes: 28 additions & 0 deletions MongoDB.Net-Tests/TestCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public void TestFindOneNotThere(){
Assert.IsNull(result);
}

[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();
Expand Down Expand Up @@ -56,6 +63,27 @@ public void TestFindGTRange(){
}
}

[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();
Expand Down
3 changes: 2 additions & 1 deletion MongoDB.Net-Tests/TestCursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions MongoDB.Net-Tests/test-data/tests.smallreads.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
8 changes: 8 additions & 0 deletions MongoDBDriver/Bson/BsonConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -48,6 +50,8 @@ public static BsonType From(Object val){
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));
}
Expand Down Expand Up @@ -110,6 +114,10 @@ public static BsonCodeWScope From(CodeWScope val){
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){
Expand Down
6 changes: 3 additions & 3 deletions MongoDBDriver/Bson/BsonNullUndefined.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;

using MongoDB.Driver;

namespace MongoDB.Driver.Bson
{
/// <summary>
Expand All @@ -19,8 +21,6 @@ public abstract byte TypeNum{
get;
}



public int Read(BsonReader reader){
return 0;
}
Expand All @@ -30,7 +30,7 @@ public void Write(BsonWriter writer){
}

public object ToNative(){
return null;
return MongoDBNull.Value;
}
}

Expand Down
6 changes: 6 additions & 0 deletions MongoDBDriver/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public ICursor FindAll() {
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);
}
Expand Down
2 changes: 1 addition & 1 deletion MongoDBDriver/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Database(Connection conn, String name){

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
Expand Down
1 change: 1 addition & 0 deletions MongoDBDriver/IMongoCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions MongoDBDriver/MongoDB.Driver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
19 changes: 19 additions & 0 deletions MongoDBDriver/MongoDBNull.cs
Original file line number Diff line number Diff line change
@@ -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.