【问题标题】:Autoincrement a meteor simpleschema member自动增加流星简单模式成员
【发布时间】:2015-06-24 04:31:36
【问题描述】:

这是我想要做的:

SimpleSchema.FaqSchema = new SimpleSchema
  order:
    type: Number
    autoValue: ->
      # somehow increment this by 1
  updatedAt:
    type: Date
    autoValue: ->
      new Date
  updatedBy:
    type: String
    autoValue: ->
      Meteor.userId()
  question: type: String
  answer: type: String

不幸的是,Meteor 文档或 simpleschema 文档中没有任何内容解释如何执行此操作。这里有 mongo 文档:http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/

但是,这并没有真正的帮助。

感谢任何帮助。架构在 coffeescript 中,但可以使用 http://js2.coffee/

进行转换

【问题讨论】:

    标签: mongodb meteor coffeescript


    【解决方案1】:

    在服务器端创建一个 Meteor 方法,在插入期间将 order 字段增加 1。此方法使用 meteor-mongo-counter 包,该包实现了 MongoDB 文档中描述的“计数器集合”技术Create an Auto-Incrementing Sequence Field

    服务器

    Meteor.methods
        "insertDocument": (doc) ->
            doc.order = incrementCounter "order"
            MyCollection.insert doc
            doc.order
    

    客户

    doc = 
        question: "Question 1"
        answer: "Answer 1"
    
    # Instead of inserting with Collection.insert doc, use Meteor.call instead
    
    Meteor.call "insertDocument", doc, (err, result) ->
        if result console.log "Inserted order number #{result}" 
    

    【讨论】:

    • 没想到原子操作。很好的发现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多