0

我想存储一个键值对对象数组。我正在使用流星和 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实例

谢谢

4

1 回答 1

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"}
                ]
    })
于 2014-11-14T08:52:31.753 回答