我有以下结构:
public abstract class DbEntity
{
[BsonId]
public ObjectId Id { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
public DateTime CreatedAt { get; set; }
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
public DateTime ModifiedAt { get; set; }
[BsonDefaultValue(EntityState.Active)]
public EntityState State { get; set; }
[BsonIgnoreIfNull]
[BsonDateTimeOptions(Kind = DateTimeKind.Utc)]
public DateTime? DeletedOn { get; set; }
}
public class Post : DbEntity
{
public ObjectId UserId { get; set; }
public string SourceUrl { get; set; }
public Guid? PictureId { get; set; }
public PostType Type { get; set; }
public string Caption { get; set; }
public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location { get; set; }
public string LocationName { get; set; }
public List<ObjectId> TaggedFriends { get; set; }
public List<string> Tags { get; set; }
public List<string> GearTags { get; set; }
public Activity Activity { get; set; }
public int LikesAmount { get; set; }
public int CommentsAmount { get; set; }
public List<SharingTarget> Shared { get; set; }
public List<string> SearchTokens
{
get
{
var result = new List<string>();
result.AddRange(Caption.GetTokens());
result.AddRange(Tags);
result.AddRange(GearTags);
result.AddRange(LocationName.GetTokens());
return result;
}
set { }
}
}
private class AggregatedPost : Followings
{
public AggregatedPost(ObjectId ownerId) : base(ownerId)
{
}
public List<Post> Posts { get; set; }
}
private class UnwindPost : Followings
{
public UnwindPost(ObjectId ownerId) : base(ownerId)
{
}
public Post Posts { get; set; }
}
我运行查询作为投影使用表达式<Func<UnwindPost,Post>>
var query = await
followingsCollection.Aggregate()
.Match(Builders<Followings>.Filter.Where(v => v.Id == userId))
.Unwind<Followings>(new ExpressionFieldDefinition<Followings>(LocalFollowingFieldName))
.Lookup(postCollection, new ExpressionFieldDefinition<Followings>(LocalFollowingFieldName), new ExpressionFieldDefinition<Post>(ForeignPostFieldName),
new ExpressionFieldDefinition<AggregatedPost>(AggregatedPostAliasFieldName))
.Unwind<UnwindPost>(new ExpressionFieldDefinition<AggregatedPost>(UnwindPostFieldName))
.Limit(pageInfo.Take)
.Skip(pageInfo.Skip)
.Project(v => new Post
{
Id = v.Posts.Id,
UserId = v.Posts.UserId,
CreatedAt = v.Posts.CreatedAt,
SourceUrl = v.Posts.SourceUrl,
Type = v.Posts.Type,
Caption = v.Posts.Caption,
Activity = v.Posts.Activity,
LocationName = v.Posts.LocationName,
LikesAmount = v.Posts.LikesAmount,
CommentsAmount = v.Posts.CommentsAmount,
PictureId = v.Posts.PictureId
}).ToListAsync();
结果我有一个例外 元素&#39; Id&#39;与类的任何字段或属性都不匹配。 如果我在mongodb上运行此查询,它将返回下一条记录:
Id: ObjectId("57d91c5393846c3c14792522")
UserId: ObjectId("57d91c5293846c3c14792512")
CreatedAt: 09/14/2016 12:45:55 PM (+0300)
SourceUrl: "test url"
Type: 0
Caption: "Test1"
Activity: NULL
LocationName: "TestLocationName"
LikesAmount: 0
CommentsAmount: 0
PictureId: NULL
如您所见,此处没有字段_id
,但ID会以Id
的形式返回。我的所有记录都存储在具有_id
字段的mongodb中。
如何在mongo语法中使用_id
字段投影记录,该字段将正确映射到我的Post类(最好使用以Expression为参数的投影)?