1

I am using fluent mongo.

I have an entity as

public class SomeList
{
        public List<ItemLike> Likes { get; set; }
        public List<ItemComment> Comments { get; set; }
        public List<ListItem> ListItems { get; set; }
}

the ListItem is another entity

public class ListItem 
{
        pub string ListItemId { get; set; }
        public List<ItemComment> CommentsList { get; set; }
        public List<Photo> ItemPhotos { get; set; }
}

So the main entity SomeList has List and also the entity ListItem has List.

I want a mongo query to get the List from ListItem.

I am using a following query

var myCollection = GetMongoDatabase().GetCollection<SomeList>("SomeLists");
var list = myCollection.Find(Query.EQ("ListItems.ListItemId", listItemId)).SetFields(Fields.Slice("ListItems.LikesList", 0)).SingleOrDefault();

ListItem listItem = list.ListItems.Where(x => x.ListItemId == listItemId).SingleOrDefault();
items = listItem.CommentsList;

When I do Query.EQ(“ListItems.ListItemId”, listItemId), it gives the main SomeList entity. I am again applying where clause in the next line. Which i don't want.

Adam Comerford
  • 21,336
  • 4
  • 65
  • 85
Devesh Tipe
  • 141
  • 10

1 Answers1

1

So you are geting the main SomeList collection returned in list here?

var list = myCollection.Find(Query.EQ("ListItems.ListItemId", listItemId))
             .SetFields(Fields.Slice("ListItems.LikesList", 0)).SingleOrDefault();

What do you get if you remove the SingleOrDefault() call from this?

Arj
  • 1,981
  • 4
  • 25
  • 45
geakie
  • 1,458
  • 9
  • 9
  • Even then it gives the main entity. – Devesh Tipe Feb 29 '12 at 18:32
  • What is returned if you remove the slice? `var list = myCollection.Find(Query.EQ("ListItems.ListItemId",listItemId));` What are you getting back - is this query working as expected? – geakie Mar 01 '12 at 10:21