Skip to content

Instantly share code, notes, and snippets.

@dphoebus
Forked from chrisnicola/MongoHelper.cs
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dphoebus/4687962d6d328f50615e to your computer and use it in GitHub Desktop.
Save dphoebus/4687962d6d328f50615e to your computer and use it in GitHub Desktop.
public static bool SameAs(this BsonDocument source, BsonDocument other, params string[] ignoreFields)
{
var elements = source.Elements.Where(x => !ignoreFields.Contains(x.Name)).ToArray();
if (elements.Length == 0 && other.Elements.Where(x => !ignoreFields.Contains(x.Name)).Any()) return false;
foreach (var element in source.Elements)
{
if (ignoreFields.Contains(element.Name)) continue;
if (!other.Names.Contains(element.Name)) return false;
var value = element.Value;
var otherValue = other[element.Name];
if (!value.SameAs(otherValue)) return false;
}
return true;
}
public static bool SameAs(this BsonValue value, BsonValue otherValue)
{
if (value.IsBsonDocument)
{
if (!otherValue.IsBsonDocument) return false;
if (!value.AsBsonDocument.SameAs(otherValue.AsBsonDocument)) return false;
}
else if (value.IsBsonArray)
{
if (!otherValue.IsBsonArray) return false;
if (value.AsBsonArray.Count != otherValue.AsBsonArray.Count) return false;
var array = value.AsBsonArray.OrderBy(x => x).ToArray();
var otherArray = otherValue.AsBsonArray.OrderBy(x => x).ToArray();
return !array.Where((t, i) => !t.SameAs(otherArray[i])).Any();
}
else return value.Equals(otherValue);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment