2014-01-23 35 views
0

我想存儲一組鍵值對對象。我正在使用流星和collection2。我聲明瞭下面給出的字段數據類型。我想存儲像如何在流星Collection2中存儲鍵值對字段

[{key:value,key:value},{key:value,key:value},{key:value,key:value}] 

對象在我的數據庫架構我定義的架構類似這樣的

Meteor.mytable = new Meteor.Collection2('mytable', { 
    schema: { 
     name: { 
      type: String, 
      label: "name", 
      optional:true 
     },    
     the_object:{ 
      type: [Object], 
      label:" Storing the list objects ", 
      optional:false 
     }, 
    } 
}); 

,並同時存儲在服務器端的數據我在做什麼

Meteor.mytable.insert({name:"name",the_object:[{key:value,key:value},{key:value,key:value}]); 

但這裏它創建一個只包含名稱字段的實例,但不包含the_object提交的

謝謝

+0

閱讀 http:// doc s.mongodb.org/manual/reference/bson-types/ http://docs.mongodb.org/meta-driver/latest/legacy/bson/ –

+0

Thanx Denis Nikanorow,我使用Object數據類型來存儲對象字段但它不存儲給定的對象。可能是我做錯了。我按照要求編輯了問題 –

回答

0

在模式中,您應該聲明密鑰名稱。您可以將關鍵值對對象存儲在collection2模式中。

Meteor.mytable = new Meteor.Collection2('mytable', { 
schema: { 
    name: { 
     type: String, 
     label: "name", 
     optional:true 
    },    
    the_object:{ 
     type: [Object], 
     label:" Storing the list objects ", 
     optional:false 
    }, 
    "the_object.$.label": { 
     type: String, 
     optional: true 
    }, 
    "the_object.$.title": { 
     type: String, 
     optional: true 
    },  
} 
}); 

現在你可以使用插入查詢,如下

Meteor.mytable.insert({name:"name",the_object:[{label:value,title:value},{label:value,title:value}]); 
Meteor.mytable.insert({name:"name",the_object:[{label:"testLable1",title:"testTitle1"},{label:"testLable2",title:"testTitle2"}]); 

我聲明如下礦山代碼collection2,見示例代碼

Actions = new Meteor.Collection("actions"); 

var Schemas = {}; 
Schemas.Action = new SimpleSchema({ 
    serviceId: { 
    type: String 
    }, 
    actionName: { 
    type: String 
    }, 
    actionDescription: { 
    type: String 
    }, 
    actionUrl: { 
    type: String, 
    optional: true 
    }, 
    actionData: { 
     type: [Object], 
     optional: true 
    }, 
    "actionData.$.label": { 
     type: String, 
     optional: true 
    }, 
    "actionData.$.require": { 
     type: String, 
     optional: true 
    }, 
    "actionData.$.name": { 
     type: String, 
     optional: true 
    }, 
    "actionData.$.placeHolder": { 
     type: String, 
     optional: true 
    }, 
    "actionData.$.actionType": { 
     type: String, 
     optional: true 
    }, 
    headers: { 
     type: Object, 
     optional: true 
    } 
}); 

Actions.attachSchema(Schemas.Action); 

礦山查詢如下

Actions.insert({ 
     serviceId:"123456", 
     actionName: "Post", 
     actionDescription: "Create a new post on your page.", 
     actionUrl: "https://graph.test.com/v2.1/me/feed", 
     actionData: 
       [ 
        {label: "Message", require: "required", name: "message", placeHolder: "Message text"}, 
        {label: "Link you want to share", require: "optional", name: "link", placeHolder: "Publicly accessible URL"}, 
        {label: "Link name", require: "optional", name: "name", placeHolder: "Title of the link preview"}, 
        {label: "Link preview picture", require: "optional", name: "picture", placeHolder: "Preview image associated with the link"}, 
        {label: "Link caption", require: "optional", name: "caption", placeHolder: "Caption under the title in the link preview"}, 
        {label: "Link description", require: "optional", name: "description", placeHolder: "Description in the link preview"}, 
        {label: "Link description", require: "optional", name: "description", placeHolder: "Description in the link preview"} 
       ] 
    }) 

Actions.insert({ 
     serviceId:"123456", 
     actionName: "Post", 
     actionDescription: "Create a new photo", 
     actionUrl: "https://graph.test.com/v2.1/me/photos", 
     actionData: 
       [ 
        {label: "Message", require: "required", name: "message", placeHolder: "Message text"}, 
        {label: "Image URL(Publicly accessible URL we can pull the image from.)", require: "required", name: "url", placeHolder: "Publicly accessbile URL of image", actionType: "createfile"} 
       ] 
    }) 
相關問題