Reputation: 3368
I have logbook documents with a list of logs embedded:
{
type:'logbook',
name:'my book',
userRef:2,
cdate: ....,
logs: [
{
color: 'red',
weight: 200,
cdate: ...,
foo: 'bar'
},
{
color: 'blue',
weight: 100,
cdate: ...,
foo: 'bar'
},
{
color: 'green',
weight: 240,
cdate: ...,
foo: 'bar'
}
]
I would like to show paginated ordered log entries for a given logbook.
Is it possible to extract those from such a structure with mongo ?
If not should I have a logEntries collection for logs instead ?
Thanks
Upvotes: 0
Views: 320
Reputation: 32184
You can specify which parts of a document you want to retrieve by using field selection. To select part of an array field, you can use the $slice
operator, for example:
// select the name and a range of log entries from the document
db.logbooks.find({ name: "my book" }, { name: 1, logs: { $slice: [10, 5] } })
Note that any sorting of the log entries besides insertion order has to be done client-side.
Upvotes: 1