在Meteor Mongo中设置字段值

时间:2016-02-21 03:37:13

标签: mongodb meteor

 Accounting = new Mongo.Collection('accounting');
 var AccountingSchema = new SimpleSchema({

        "caseID":{
          label:"Case",
          type: String,
          autoform: {
            type: "universe-select",
            afFieldInput: {uniPlaceholder:"Select..."},
            options: function(){
              return Cases.find().map(function (c) {
                  return {label: c.caseName, value: c._id};
              });
            }
          }
        },
        caseName:{
          type: String,
          optional: true
        },
        "category":{ 
        type: String,
        autoform: {
            afFieldInput: {
              type: "universe-select",
              uniPlaceholder:"Select..."
            },
            options: 
            [
               {label: "Personal", value: "Personal"},
               {label: "Therapy", value: "Therapy"},
               {label: "Other", value: "Other"}  
            ]
          }
        },
       "cost":{ 
        type: Number
        },
       "description":{
        type: String,
        max: 100,
        optional: true,
        autoform: {
        afFieldInput: {
         type: "textarea",
         rows: 5
         }
        }
       },
       "accountingFiles": {
             type: [String],
             optional: true,
             autoform: {
              type: 'hidden'
             },
             autoValue: function(){       
              if (this.isInsert) {
                return this.value;
              } else if (this.isUpsert) {
                return {$setOnInsert: this.value};
              }
             }
       },
       "expenses":{
            type: [Object],
            optional: true,
            autoValue: function() {            
              var caseID      = this.field('caseID');
              var category    = this.field('category');
              var cost        = this.field('cost');
              var description = this.field('description');
              var accountingFiles = this.field('accountingFiles');
              var seq = 1; 
              accountingFiles.value = (!!accountingFiles.value ? accountingFiles.value:[]);


                if(this.isInsert){

                   return [{
                      "_id":Random.id(),
                      date: new Date(),
                      category: category.value,
                      cost: cost.value,
                      seq:seq,
                      description : description.value,
                      accountingFiles: accountingFiles.value,
                      createdBy: this.userId
                    }];

                } else {

                    return {
                     $push: {

                        "_id":Random.id(),
                        date: new Date(),
                        category: category.value,
                        cost: cost.value,
                        seq:seq,
                        description : description.value,
                        accountingFiles: accountingFiles.value,
                        createdBy: this.userId

                     }
                   };
                }
           }
         },
        "expenses.$._id":{ 
          type: String
        },
        "expenses.$.seq":{
            type: Number,
            optional: true
        },
        'expenses.$.date': {
          type: Date,
          optional: true
        },
        "expenses.$.category":{ 
          type: String
        },
        "expenses.$.cost":{ 
          type: Number,
          optional: true,
        },
        'expenses.$.description': {
          type: String,
          optional: true
        },
        "expenses.$.accountingFiles":{
           type: [String],
           optional: true
        },
        'expenses.$.createdBy':{
            type: String,
            optional: true
        },
        "grandTotal":{
           type: Number,
           optional: true
        },
        "createdBy":{
          type: String,
          label: "Created By",
          optional: true,
          autoValue: function() {
            if (this.isInsert) {
              return this.userId;
            } else if (this.isUpsert) {
              return {$setOnInsert:this.userId};
            } else {
              this.unset();  // Prevent user from supplying their own value
            }
          }
        },
        "updatedBy":{
          type: String,
          optional: true,
          label: "Updted By",
          autoValue: function() {
            if (this.isUpdate) {
              return this.userId;
            }
          },
          denyInsert: true,
        },
         "createdAt": {
          optional: true,
          type: Date,
          autoValue: function() {
            if (this.isInsert) {
              return new Date; 
            } else if (this.isUpsert) {
              return {$setOnInsert: new Date};
            } else {
              this.unset();  // Prevent user from supplying their own value
            }
          }
        },
        "updatedAt": {
          type: Date,
          optional: true,
          autoValue: function() {
            if (this.isUpdate) {
             return new Date();

            }
          },
          denyInsert: true,
          optional: true
        }

      });


      Accounting.attachSchema( AccountingSchema );

        //server method
        addAccounting: function(doc){
        check(doc, Accounting.simpleSchema());

        //add auto incrmenting counters for each expense
        Meteor.call('checkFirstIndex','expenses');
        doc.expenses[0].seq = Meteor.call('getNextSequence','expenses');


        return Accounting.insert(doc);        


      }

我正在使用一种方法来插入文档:

methodName: function(doc){
 doc.someObject.$.element = someValue; //this gives an error
 table.insert(doc);
}

我试图在插入文件之前设置“someObject”对象的“元素”但是收到错误。如何才能做到这一点?

注意:我在原始问题中犯了错误。 someObject的类型应该是一个对象数组。我纠正了它。

注意:我已上传完整的代码集。我的想法是保留caseID的所有条目,而不是有两个表。何时上传文档都会在费用对象下添加。但是我想在服务器上设置开销。$。seq。这是mongodb相当于自动增量字段。我正在使用autoForm。

1 个答案:

答案 0 :(得分:2)

我认为您希望将架构更改为此。

someObject:{
    type: Object 
}, 
"someObject.element":{ 
    type: String
 }

使用$是占位符,仅用于数组(我相信)

https://github.com/aldeed/meteor-simple-schema/blob/master/README.md#schema-keys

编辑:问题更新后更新的答案

好的,根据您的更改,我认为您遇到的问题是您尝试使用" $"来获取数组值。 " $"用作模式中的占位符,但它不是模式之外的有效javascript。

试试这个。

methodName: function(doc){
 doc.someObject[0]element = someValue;
 table.insert(doc);
}

如果" someValue"变量假设是一个数组然后执行此操作。

methodName: function(doc){
 doc.someObject = someValue;
 table.insert(doc);
}