开发者

Mongo collection doc organisation and query

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: 'bl开发者_开发百科ue',
          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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜