Insert if no document, else update autoform

I have a shipping address database belonging to each user, they should be able to insert if no doc or update if there’s a doc. How can I go about this using autoform and simple schema?

Managed to do it in a rather hacky way


Template.address.onCreated(function onCreated() {
  Tracker.autorun(() => {
  address() {
    return Address.findOne({
      owner: Meteor.userId(),
  isAddress() {
    if (Address.find().count() === 0) {
      Session.set('isAddress', false);
    } else {
      Session.set('isAddress', true);
    return Session.get('isAddress');


<template name="address">
	{{#if isAddress}}
		<h2>Edit your shipping address</h2>
		{{> quickForm collection="Address" doc=address id="updateAddressForm" type="update"}}
		<h2>Add a shipping address</h2>
		{{> quickForm collection="Address" id="insertAddressForm" type="insert"}}

“insert if exists, otherwise update” is called an “upsert”. See the docs on Meteor’s Collection.upsert. Here’s a thread talking about autoform + upsert, and here’s the autoform docs on the topic

1 Like