Skip to content

Commit dfb2079

Browse files
committed
Added AddToset,Push,Pull,Pop
Added overload to Set that accepts a fieldname and a value
1 parent 56d9ab1 commit dfb2079

File tree

1 file changed

+225
-2
lines changed

1 file changed

+225
-2
lines changed

Query/MongoQuery.cs

Lines changed: 225 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,229 @@ public long Count() {
401401

402402
#region Updating
403403

404+
#region PullAll
405+
/// <summary>
406+
/// removes all occurrences of each value in value_array from field, if field is an array. If field is present but is not an array, an error condition is raised.
407+
/// </summary>
408+
/// <param name="field"></param>
409+
/// <param name="value"></param>
410+
public void PullAll(string field, object value)
411+
{
412+
var doc = new BsonDocument();
413+
doc[field] = value;
414+
PullAll(doc);
415+
}
416+
/// <summary>
417+
/// removes all occurrences of each value in value_array from field, if field is an array. If field is present but is not an array, an error condition is raised.
418+
/// </summary>
419+
/// <param name="document"></param>
420+
public void PullAll(object document)
421+
{
422+
PullAll(new BsonDocument(document));
423+
}
424+
/// <summary>
425+
/// removes all occurrences of each value in value_array from field, if field is an array. If field is present but is not an array, an error condition is raised.
426+
/// </summary>
427+
/// <param name="document"></param>
428+
public void PullAll(BsonDocument document)
429+
{
430+
_SendUpdate("$pullAll", UpdateOptionTypes.MultiUpdate, document);
431+
}
432+
433+
#endregion
434+
435+
#region Pull
436+
/// <summary>
437+
/// removes all occurrences of value from field, if field is an array. If field is present but is not an array, an error condition is raised.
438+
/// </summary>
439+
/// <param name="field"></param>
440+
/// <param name="value"></param>
441+
public void Pull(string field, object value)
442+
{
443+
var doc = new BsonDocument();
444+
doc[field] = value;
445+
Pull(doc);
446+
}
447+
/// <summary>
448+
/// removes all occurrences of value from field, if field is an array. If field is present but is not an array, an error condition is raised.
449+
/// </summary>
450+
/// <param name="document"></param>
451+
public void Pull(object document)
452+
{
453+
Pull(new BsonDocument(document));
454+
}
455+
/// <summary>
456+
/// removes all occurrences of value from field, if field is an array. If field is present but is not an array, an error condition is raised.
457+
/// </summary>
458+
/// <param name="document"></param>
459+
public void Pull(BsonDocument document)
460+
{
461+
_SendUpdate("$pull", UpdateOptionTypes.MultiUpdate, document);
462+
}
463+
464+
#endregion
465+
466+
#region Add To Set
467+
/// <summary>
468+
/// Adds value to the array only if its not in the array already.
469+
/// </summary>
470+
/// <param name="field"></param>
471+
/// <param name="value"></param>
472+
public void AddToSet(string field, object value)
473+
{
474+
var doc = new BsonDocument();
475+
doc[field] = value;
476+
AddToSet(doc);
477+
}
478+
/// <summary>
479+
/// Adds value to the array only if its not in the array already.
480+
/// </summary>
481+
/// <param name="document"></param>
482+
public void AddToSet(object document)
483+
{
484+
AddToSet(new BsonDocument(document));
485+
}
486+
/// <summary>
487+
/// Adds value to the array only if its not in the array already.
488+
/// </summary>
489+
/// <param name="document"></param>
490+
public void AddToSet(BsonDocument document)
491+
{
492+
_SendUpdate("$addToSet", UpdateOptionTypes.MultiUpdate, document);
493+
}
494+
#endregion
495+
496+
#region Add All To Set
497+
/// <summary>
498+
/// Adds values to the array only if its not in the array already.
499+
/// </summary>
500+
/// <param name="field"></param>
501+
/// <param name="values"></param>
502+
public void AddAllToSet(string field, IEnumerable values)
503+
{
504+
var innerDoc = new BsonDocument();
505+
innerDoc["$each"] = values;
506+
var doc = new BsonDocument();
507+
doc[field] = innerDoc;
508+
AddAllToSet(doc);
509+
}
510+
/// <summary>
511+
/// Adds values to the array only if its not in the array already.
512+
/// </summary>
513+
/// <param name="field"></param>
514+
/// <param name="values"></param>
515+
public void AddAllToSet(string field, params object[] values)
516+
{
517+
var innerDoc = new BsonDocument();
518+
innerDoc["$each"] = values;
519+
var doc = new BsonDocument();
520+
doc[field] = innerDoc;
521+
AddAllToSet(doc);
522+
}
523+
/// <summary>
524+
/// Adds value to the array only if its not in the array already.
525+
/// </summary>
526+
/// <param name="document"></param>
527+
public void AddAllToSet(BsonDocument document)
528+
{
529+
_SendUpdate("$addToSet", UpdateOptionTypes.MultiUpdate, document);
530+
}
531+
#endregion
532+
533+
#region Push
534+
/// <summary>
535+
/// appends value to field, if field is an existing array, otherwise sets field to the array [value] if field is not present. If field is present but is not an array, an error condition is raised.
536+
/// </summary>
537+
/// <param name="field"></param>
538+
/// <param name="value"></param>
539+
public void Push(string field, object value)
540+
{
541+
var doc = new BsonDocument();
542+
doc[field] = value;
543+
Push(doc);
544+
}
545+
/// <summary>
546+
/// appends value to field, if field is an existing array, otherwise sets field to the array [value] if field is not present. If field is present but is not an array, an error condition is raised.
547+
/// </summary>
548+
/// <param name="document"></param>
549+
public void Push(object document)
550+
{
551+
Push(new BsonDocument(document));
552+
}
553+
/// <summary>
554+
/// appends value to field, if field is an existing array, otherwise sets field to the array [value] if field is not present. If field is present but is not an array, an error condition is raised.
555+
/// </summary>
556+
/// <param name="document"></param>
557+
public void Push(BsonDocument document)
558+
{
559+
_SendUpdate("$push", UpdateOptionTypes.MultiUpdate, document);
560+
}
561+
562+
#endregion
563+
564+
#region PushAll
565+
/// <summary>
566+
/// appends each value in value_array to field, if field is an existing array, otherwise sets field to the array value_array if field is not present. If field is present but is not an array, an error condition is raised.
567+
/// </summary>
568+
/// <param name="field"></param>
569+
/// <param name="value"></param>
570+
public void PushAll(string field, object value)
571+
{
572+
var doc = new BsonDocument();
573+
doc[field] = value;
574+
PushAll(doc);
575+
}
576+
/// <summary>
577+
/// appends each value in value_array to field, if field is an existing array, otherwise sets field to the array value_array if field is not present. If field is present but is not an array, an error condition is raised.
578+
/// </summary>
579+
/// <param name="document"></param>
580+
public void PushAll(object document)
581+
{
582+
PushAll(new BsonDocument(document));
583+
}
584+
/// <summary>
585+
/// appends each value in value_array to field, if field is an existing array, otherwise sets field to the array value_array if field is not present. If field is present but is not an array, an error condition is raised.
586+
/// </summary>
587+
/// <param name="document"></param>
588+
public void PushAll(BsonDocument document)
589+
{
590+
_SendUpdate("$pushAll", UpdateOptionTypes.MultiUpdate, document);
591+
}
592+
#endregion
593+
594+
#region Pop
595+
/// <summary>
596+
/// removes the last element in an array
597+
/// </summary>
598+
/// <param name="field"></param>
599+
public void Pop(string field)
600+
{
601+
Pop(field, false);
602+
}
603+
/// <summary>
604+
/// Removes all matching fields from each document in the query
605+
/// </summary>
606+
public void Pop(string field, bool first)
607+
{
608+
//mark the fields to be removed
609+
var remove = new BsonDocument();
610+
remove[field] = first ? -1 : 1;
611+
//send the command
612+
_SendUpdate("$pop", UpdateOptionTypes.MultiUpdate, remove);
613+
}
614+
#endregion
615+
616+
/// <summary>
617+
/// Updates the matching record with with values provided or adds the new item to the object entirely
618+
/// </summary>
619+
/// <param name="field"></param>
620+
/// <param name="value"></param>
621+
public void Set(string field,object value)
622+
{
623+
var doc = new BsonDocument();
624+
doc[field] = value;
625+
Set(doc);
626+
}
404627
/// <summary>
405628
/// Updates all matching records with the values on the provided object
406629
/// or adds the new item to object entirely
@@ -439,8 +662,8 @@ public void Unset(params string[] fields) {
439662
public void Increment(params string[] fields) {
440663

441664
//create the document
442-
BsonDocument document = new BsonDocument();
443-
foreach (string field in fields) {
665+
BsonDocument document = new BsonDocument{UseRawFieldNames = true};
666+
foreach (var field in fields) {
444667
document.Set<int>(field, 1);
445668
}
446669

0 commit comments

Comments
 (0)