2014-01-23 5 views
0

Я хочу сохранить массив объектов пары значений ключа. Я использую метеорит и сборник2. Я объявил тип данных поля, как указано ниже. Я хочу сохранить объект какКак сохранить поле пары ключевых значений в meteor 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"} 
       ] 
    }) 
Смежные вопросы