-1

I need to get last 12 entries from ListOfItems of a Block whose Name is "corner":

public class Block {
    public ObjectId Id { get; set; }
    public string Name;
    public List<Items> ListOfItems;
}

But since there can be many many items in ListOfItems, all operations must be made on the DB server, not locally. Is it even possible, btw?

How to do it?

Conclusion:

It's not possible, but here is the real discovery: linking, instead of embedding

Community
  • 1
  • 1
Tar
  • 8,529
  • 9
  • 56
  • 127
  • 1
    I don't think this is possible. You'd need to pull an entire `Block` out of Mongo in order to access `ListOfItems`. I think your DB schema is the problem here. – Nate Barbettini Jun 05 '16 at 18:01
  • @NateBarbettini, what do you suggest? should `ListOfItems` be external to `Block`, with keys that bind them together? (like with SQL?) – Tar Jun 05 '16 at 18:32
  • Is ListOfItems embedded or referenced? – Paul Jun 05 '16 at 18:36
  • @Paul, embedded. Actually I'm not very experienced with linking, is this the classical case to use that feature? – Tar Jun 05 '16 at 18:42
  • 1
    Yes. Generally, if you're familiar with Domain Driven Design, you'd Embed 'value objects' (i.e. things whose life doesn't matter outside the context, e.g. the value of a person's email address; you don't need to change track it you just need the current one(s)), and Link 'entities' (things which have meaning outside the context, e.g. perhaps an Address may go through multiple owners in a given system, and so you might track that in its own collection). – Paul Jun 05 '16 at 18:46
  • 1
    Sorry, getting long. Also, as a general rule, you Embed things if you'll always want them to be fetched at the same time as the containing object. If you sometimes want them and sometimes don't, that's OK too, but if you have a list and only want some of them based on some query parameters, that's starting to indicate that they have contextual meaning of their own, and you might consider splitting them out into their own collection. – Paul Jun 05 '16 at 18:47
  • @Paul, you ought to add an answer, it sounds like the solution I was looking for.. but for general knowledge, is it not possible, as [NateBarbettini said above](http://stackoverflow.com/questions/37644090/how-to-create-a-query-to-get-only-n-entries-from-a-list-in-certain-record-in-mon#comment62770019_37644090)? – Tar Jun 05 '16 at 18:53

1 Answers1

2

ListOfItems should probably be referencing another collection, rather than be an embedded array. You can either include or not include fields (including sub-documents) when querying MongoDB, but I'm not aware of a way to filter, limit, or skip items in the subdocument on the MongoDB side.

Generally, if you're familiar with Domain Driven Design, you'd Embed 'value objects' (i.e. things whose life doesn't matter outside the context, e.g. the value of a person's email address; you don't need to change track it you just need the current one(s)), and Link 'entities' (things which have meaning outside the context, e.g. perhaps an Address may go through multiple owners in a given system, and so you might track that in its own collection).

Also, as a general rule, you Embed things if you'll always want them to be fetched at the same time as the containing object. If you sometimes want them and sometimes don't, that's OK too, but if you have a list and only want some of them based on some query parameters, that's starting to indicate that they have contextual meaning of their own, and you might consider splitting them out into their own collection.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • 1
    Just to add the new thing I learned in this post: [the capability to reference entities instead of embedding them, via DBRef](http://stackoverflow.com/a/15248058/587467) – Tar Jun 05 '16 at 21:12