How to define a generic nested object in Mongoose

mongodb, mongoose, node.js

Solution

Use the Mixed type, which allows you to store the arbitrary sub-objects in your example.

var Activity = new Schema({
    date : Date
  , user : String 
  , action : String
  , detail : Mixed
})

Problem

I would like to have a nested object in the detail for the activiy log. See the example. How do I define the schema in mongoose? ``` activity: { date: '1/1/2012' , user: 'benbittly', action: 'newIdea', detail: { 'title': 'how to nest' , 'url': '/path/to/idea' } activity: { date: '1/2/2012' , user: 'susyq', action: 'editProfile', detail: { 'displayName': 'Susan Q' , 'profileImageSize': '32' , 'profileImage': '/path/to/image' } ```

Original source