Skip to content

Commit 57862cc

Browse files
committed
Added Find and Modify. This has allowed for the generation of incremental values for documents
1 parent 9204764 commit 57862cc

15 files changed

+360
-35
lines changed

Bson/BsonAnonymousTypeParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public object ReadType(string parent, object section) {
7272
{
7373

7474
//try and get the value to use
75-
value = this._Data.Get(path, value);
75+
value = _Data.Get(path, value);
7676
}
7777

7878
//add this to the list of values

Bson/BsonDocument.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ public static BsonDocument FromStream(Stream stream) {
135135

136136
#endregion
137137

138+
/// <summary>
139+
/// Appends the specified field and value to the document and returns the original collection.
140+
/// </summary>
141+
/// <param name="field">The field to set.</param>
142+
/// <param name="document">The document to use as the value.</param>
143+
/// <returns></returns>
144+
public BsonDocument AppendField(string field, object document)
145+
{
146+
this[field] = document;
147+
return this;
148+
}
138149
}
139150

140151
}

Bson/BsonObject.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public T Get<T>(T template)
9797
/// <summary>
9898
/// Uses an anonymous type as a template for the values to return
9999
/// </summary>
100-
public T GetWithId<T>(T template,string idField)
100+
public T GetWithId<T>(T template,string idField="id")
101101
{
102102
return BsonAnonymousTypeParser.IsAnonymousType(template) ? BsonAnonymousTypeParser.PopulateAnonymousTypeWithId(this, template,idField) : template;
103103
}
@@ -116,13 +116,14 @@ public T Get<T>(string field) {
116116
}
117117

118118
/// <summary>
119-
/// Handles getting the value from this DynamicCObject
119+
/// Handles getting the value from this DynamicCObject.
120+
/// The convert function has been updated for arrays to help with the conversion
120121
/// </summary>
121122
public T Get<T>(string field, T @default) {
122123

123124
//maps lower document levels
124125
if (BsonAnonymousTypeParser.IsAnonymousType(@default)) {
125-
return BsonAnonymousTypeParser.PopulateAnonymousType<T>(this, field, @default);
126+
return BsonAnonymousTypeParser.PopulateAnonymousType(this, field, @default);
126127
}
127128

128129
//locate the field first
@@ -131,15 +132,31 @@ public T Get<T>(string field, T @default) {
131132

132133
//if nothing is found then just return the value
133134
try {
134-
return detail.Field is MongoDataType
135-
? (T)detail.Field.Get<T>()
136-
: @default;
135+
if (detail.Field is MongoArrayType)
136+
{
137+
var value = (object[])detail.Field.Get<T>(); //todo: can this be a document array as well?
138+
//todo: at the moment, only string arrays are supported. accomodate others
139+
if (value==null) return (T) (value as object);
140+
if (@default is string[])
141+
{
142+
//var output = new List<string>();
143+
var output = (object)value.Select(x => (string) Convert.ChangeType(x, typeof (string))).ToArray();
144+
return (T) output;
145+
}
146+
//if (value != null && null != @default) value = Convert.ChangeType(value, @default.GetType());
147+
return (T)(value as object);
148+
}
149+
if (detail.Field is MongoNumberType)
150+
{
151+
var v = detail.Field.Get(@default.GetType()); //todo: what happens if @default is null
152+
return v == null ? @default : (T) v;
153+
}
154+
return (T) detail.Field.Get<T>();
137155
}
138156
//make sure they get some sort of value
139157
catch {
140158
return @default;
141159
}
142-
143160
}
144161

145162
/// <summary>

Mongo.csproj renamed to CSMongo.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<Compile Include="Responses\QueryResponse.cs" />
134134
<Compile Include="Responses\ResponseBase.cs" />
135135
<Compile Include="Results\ProfileResult.cs" />
136+
<Compile Include="Types\FindAndModifyParameters.cs" />
136137
<Compile Include="Types\MongoBinaryTypes.cs" />
137138
<Compile Include="Types\MongoDataTypes.cs" />
138139
<Compile Include="Types\OpCodeTypes.cs" />

Commands/MongoDatabaseCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,14 @@ public static DeleteCollectionIndexResult DeleteCollectionIndex(MongoDatabase da
347347
/// Executes a command against the database using the provided information
348348
/// </summary>
349349
public static CommandResponse RunCommand(MongoDatabase database, object parameters) {
350-
return MongoDatabaseCommands.RunCommand(database, new BsonObject(parameters));
350+
return RunCommand(database, new BsonObject(parameters));
351351
}
352352

353353
/// <summary>
354354
/// Executes a command against the database using the provided information
355355
/// </summary>
356356
public static CommandResponse RunCommand(MongoDatabase database, object parameters, bool expectResponse) {
357-
return MongoDatabaseCommands.RunCommand(database, parameters, true);
357+
return RunCommand(database, new BsonObject(parameters), true);
358358
}
359359

360360
/// <summary>

DataTypes/MongoDataType.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,32 @@ public abstract class MongoDataType {
5252
/// Handles assigning a value to a field
5353
/// </summary>
5454
public virtual void Set<T>(T value) {
55-
this.Value = this.ConvertValue<T>(value);
55+
Value = ConvertValue<T>(value);
5656
}
5757

5858
/// <summary>
5959
/// Handles returning a value in the requested type
6060
/// </summary>
6161
public virtual object Get<T>() {
62-
return this.ConvertValue<T>(this.Value);
62+
return ConvertValue<T>(Value);
6363
}
6464

65+
/// <summary>
66+
/// Gets the specified type.
67+
/// </summary>
68+
/// <param name="type">The type.</param>
69+
/// <returns></returns>
70+
public virtual object Get(Type type)
71+
{
72+
try
73+
{
74+
return Convert.ChangeType(Value,type);
75+
}
76+
catch
77+
{
78+
return null;
79+
}
80+
}
6581
/// <summary>
6682
/// Handles converting the passed in value to the base
6783
/// type for this Mongo object

DataTypes/MongoNumberType.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
52
using CSMongo.Bson;
63
using System.IO;
74
using CSMongo.Types;

Extensions/Queries/MongoQueryExtensions.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using CSMongo.Bson;
56

67
namespace CSMongo.Extensions.Queries {
78

@@ -18,10 +19,25 @@ public static IEnumerable<T> As<T>(this IEnumerable<MongoDocument> documents, T
1819
return documents.Select(document => document.Get(template));
1920
}
2021

22+
///// <summary>
23+
///// Selects information from the document in a specific format
24+
///// </summary>
25+
//public static IEnumerable<T> AsWithId<T>(this IEnumerable<MongoDocument> documents, T template, string idField = "Id")
26+
//{
27+
// return documents.Select(document => document.GetWithId(template, idField));
28+
//}
29+
///// <summary>
30+
///// Selects information from the document in a specific format
31+
///// </summary>
32+
//public static IEnumerable<T> As<T>(this IEnumerable<BsonDocument> documents, string start, T template)
33+
//{
34+
// return documents.Select(document => document.Get(start, template));
35+
//}
36+
2137
/// <summary>
2238
/// Selects information from the document in a specific format
2339
/// </summary>
24-
public static IEnumerable<T> AsWithId<T>(this IEnumerable<MongoDocument> documents, T template, string idField = "Id")
40+
public static IEnumerable<T> AsWithId<T>(this IEnumerable<BsonDocument> documents, T template, string idField = "Id")
2541
{
2642
return documents.Select(document => document.GetWithId(template, idField));
2743
}
@@ -32,7 +48,6 @@ public static IEnumerable<T> As<T>(this IEnumerable<MongoDocument> documents, st
3248
{
3349
return documents.Select(document => document.Get(start, template));
3450
}
35-
3651
/// <summary>
3752
/// Selects information from the document in a specific format
3853
/// </summary>

Mongo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public static class Mongo {
2525
/// </summary>
2626
public static readonly string DocumentIdKey = "_id";
2727

28+
/// <summary>
29+
/// The default field name that holds the search criteria for an upsert
30+
/// </summary>
31+
public static readonly string UpsertKey = "_upsert";
2832
/// <summary>
2933
/// Null value to use when creating new anonymous types
3034
/// since you can't assign null directly

0 commit comments

Comments
 (0)