2016-05-12 45 views
0

如何检查作者是否存在于嵌套数组中。 我的模型看起来是这样的:C#官方MongoDB连接器 - 检查用户是否已经喜欢发布

public class Likes 
{ 
    // - Id is equal to article ID 
    [Required] 
    public ObjectId Id { get; set; } 

    [Required] 
    public Like[] likes { get; set; } 
} 


public class Like 
{ 
    [Required] 
    public string author { get; set; } 

    public DateTime timestamp { get; set; } 
} 

在BSON格式如下:

{ 
    "_id" : ObjectId("5733d90aa75c955354bc2057"), 
    "likes" : [ 
     { 
      "author" : "[email protected]", 
      "timestamp" : ISODate("2016-05-12T12:27:02.315Z") 
     }, 
     { 
      "author" : "[email protected]", 
      "timestamp" : ISODate("2016-05-12T12:27:03.610Z") 
     }, 
     { 
      "author" : "[email protected]", 
      "timestamp" : ISODate("2016-05-12T12:27:04.185Z") 
     } 
    ] 
} 

正如你看到的。同一个人可以喜欢同一篇文章。我怎样才能检查,例如[email protected]是否存在于某个帖子的喜好列表中?

回答

0

在C#中,您可以简单地使用查询表达式。请按照以下方式进行检查:

var exists= ikes.likes.where(p=>p.Id='xxx').Any(x=>x.authod.Equals('author')) 
相关问题