Auto-increment simulation element meteor

Here's what I'm trying to do:

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

      

Unfortunately, there is nothing in the Meteor documentation or simpleschema docs that explains how to do this. There are mongo docs here: http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/

However, it doesn't help much.

Any help is appreciated. The schema is in coffeescript but can be converted using http://js2.coffee/

+3


source to share


1 answer


Create a server side Meteor method that increments the order field value by 1 during inserts. This method uses the meteor-mongo-counter package, which implements the "Collection of Counters" method described in the MongoDB documentation Create Automatic Incremental Sequence Field :

Server

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

      



Client

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}" 

      

+3


source







All Articles