Skip to content

Commit

Permalink
Fixing and _id when include occurs before filter
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Apr 13, 2020
1 parent 65ac3c0 commit 0bac74b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion LiteDB.Tests/Database/DbRef_Include_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void DbRef_Include()

orders.Insert(order);

orders.EnsureIndex(x => x.Customer.Id);
//orders.EnsureIndex(x => x.Customer.Id);

// query orders using customer $id
// include customer data and customer address
Expand Down
45 changes: 32 additions & 13 deletions LiteDB/Client/Mapper/BsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -460,17 +460,30 @@ private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, IT

member.Deserialize = (bson, m) =>
{
var idRef = bson.IsDocument ? bson["$id"] : BsonValue.Null;
var missing = bson.IsDocument ? (bson["$missing"] == true) : false;
// if not a document (maybe BsonValue.null) returns null
if (bson == null || bson.IsDocument == false) return null;
var doc = bson.AsDocument;
var idRef = doc["$id"];
var missing = doc["$missing"] == true;
var included = doc.ContainsKey("$ref") == false;
if (missing) return null;
return m.Deserialize(entity.ForType,
idRef.IsNull ?
bson : // if has no $id object was full loaded (via Include) - so deserialize using normal function
bson.AsDocument.ContainsKey("$type") ?
new BsonDocument { ["_id"] = idRef, ["_type"] = bson["$type"] } :
new BsonDocument { ["_id"] = idRef }); // if has $id, deserialize object using only _id object
if (included)
{
doc["_id"] = idRef;
return m.Deserialize(entity.ForType, doc);
}
else
{
return m.Deserialize(entity.ForType,
doc.ContainsKey("$type") ?
new BsonDocument { ["_id"] = idRef, ["_type"] = bson["$type"] } :
new BsonDocument { ["_id"] = idRef }); // if has $id, deserialize object using only _id object
}
};
}

Expand Down Expand Up @@ -526,20 +539,26 @@ private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, IT
foreach (var item in array)
{
var refId = item["$id"];
var missing = item["$missing"] == true;
if (item.IsDocument == false) continue;
var doc = item.AsDocument;
var idRef = doc["$id"];
var missing = doc["$missing"] == true;
var included = doc.ContainsKey("$ref") == false;
// if referece document are missing, do not inlcude on output list
if (missing) continue;
// if refId is null was included by "include" query, so "item" is full filled document
if (refId.IsNull)
if (included)
{
item["_id"] = idRef;
result.Add(item);
}
else
{
var bsonDocument = new BsonDocument { ["_id"] = refId };
var bsonDocument = new BsonDocument { ["_id"] = idRef };
if (item.AsDocument.ContainsKey("$type"))
{
Expand Down
8 changes: 6 additions & 2 deletions LiteDB/Engine/Query/Pipeline/BasePipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ void DoInclude(BsonDocument value)
// load document based on dataBlock position
var refDoc = lookup.Load(node);

value.Remove("$id");
//do not remove $id
value.Remove("$ref");

refDoc.CopyTo(value);
// copy values from refDocument into current documet (except _id - will keep $id)
foreach (var element in refDoc.Where(x => x.Key != "_id"))
{
value[element.Key] = element.Value;
}
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions LiteDB/Utils/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ internal class Constants
[Conditional("DEBUG")]
public static void LOG(string message, string category)
{
//var threadID = Environment.CurrentManagedThreadId;
//
//Debug.WriteLine(message, threadID + "|" + category);
var threadID = Environment.CurrentManagedThreadId;

Debug.WriteLine(message, threadID + "|" + category);
}

/// <summary>
Expand Down

1 comment on commit 0bac74b

@lbnascimento
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for #1625

Please sign in to comment.