English 中文(简体)
• 如何界定在Mongoose的通用封条物体
原标题:How to define a generic nested object in Mongoose

我愿详细列出活性原的物体。 See the example. 我如何界定县的图象?

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 
    }
最佳回答

使用<<>Mixed型号,使你能够把任意的次级目标列入你的例子。

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

在您的图表中注明一个任意物体(即“任何区别”),您可使用<代码>Mixed类型或单纯的<代码>{}。

var activity: new Schema({
    date: Date,
    user: String, 
    action: String, 
    detail: Schema.Types.Mixed,
    meta: {}  // equivalent to Schema.Types.Mixed

});

The catch

但是,如果增加灵活性,就会出现一种捕获。 在使用<代码>Mixed(或{><>>>>/code>时,您必须明确表明,您已经做了改动:

activity.detail.title = "title";
activity.markModified( detail );
activity.save();

Source





相关问题
Access DB Ref MongoDB

Whats the best way to access/query a DB Ref: UPDATE: users: name, groupref : {$ref:"groups",$id:"ObjectId ..." } } groups: name, topic, country,...,.. Assumption is that user belongs to only one ...

MongoDB nested sets

What re the best practices to store nested sets (like trees of comments) in MongoDB? I mean, every comment can have a parent comment and children-comments (answers). Storing them like this: { ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

MongoDB takes long for indexing

I have the following setup: Mac Pro with 2 GB of RAM (yes, not that much) MongoDB 1.1.3 64-bit 8 million entries in a single collection index for one field (integer) wanted Calling .ensureIndex(...) ...

Storing and accessing large amounts of data

My application creates pieces of data that, in xml, would look like this: <resource url="someurl"> <term> <name>somename</name> <frequency>somenumber</...