自动增加流星简单模式成员

Autoincrement a meteor simpleschema member

这是我正在尝试做的事情:

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/

进行转换

在服务器端创建一个 Meteor 方法,在插入期间将顺序字段递增 1。此方法使用 meteor-mongo-counter package which implements the "Counters Collection" technique described in the MongoDB documentation 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}"