Autoincrement a meteor simpleschema member

Adnan Y

Here is 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 Meteor documentation or simpleschema docs for this matter which explains how to do it. There is mongo docs here : http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/

However, this doesn't really help.

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

chridam

Create a Meteor method on the server side that increments the order field by 1 during inserts. This method uses the meteor-mongo-counter package which implements the "Counters Collection" technique described in the MongoDB documentation Create an Auto-Incrementing 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}" 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Meteor SimpleSchema with user dropdown

From Dev

Meteor SimpleSchema insert with nested object

From Dev

Meteor SimpleSchema Repeated Nested Objects

From Dev

Any Crud example for Meteor and react with SimpleSchema

From Dev

Validate date values in Meteor AutoForm SimpleSchema

From Dev

Meteor Autoform/SimpleSchema - quickform (type="update") not working

From Dev

Meteor server is crashing on SimpleSchema autoValue option

From Dev

Using Meteor.ObjectID as a type in SimpleSchema

From Dev

Any Crud example for Meteor and react with SimpleSchema

From Dev

How to add Meteor.userId() to SimpleSchema via autoValue?

From Dev

Avoiding redundancy when using both a SimpleSchema and a ValidatedMethod for a meteor collection?

From Dev

View simpleschema mongodb collections

From Java

PostgreSQL Autoincrement

From Dev

AUTOINCREMENT not working

From Dev

Insert with AutoIncrement

From Dev

SimpleSchema invalid keys with nested autoValue

From Dev

ObjectId in simpleSchema or collection2

From Dev

SimpleSchema and MongoDB Nested Arrays in Arrays

From Dev

How to create reusable component with simpleSchema

From Dev

SimpleSchema invalid keys "_id required"

From Dev

SimpleSchema: How to validate an specific array

From Dev

SimpleSchema: Conditionally required field is not working

From Dev

Meteor $and with $or

From Dev

Autoincrement field support in dynamoDB

From Dev

Entityframework disable autoincrement on purpose

From Dev

Liquibase not working with mysql autoincrement

From Dev

Autoincrement ID with a prefix

From Dev

Autoincrement id increase by 2

From Dev

Create column with Year and autoincrement

Related Related

HotTag

Archive