Skip to content

2A. Creating Documents

Jon Clausen edited this page Dec 20, 2015 · 6 revisions

Create a new document and then query for (we're maintaining case in this example, but it's not necessary if you've already mapped your schema properties, which maintain case automatically)

var person=this.populate({
	'first_name'='John',
	'last_name'='Doe',
	'testvar'='here',
	'address'={
		'street'='123 Anywhere Lane',
		'city'='Grand Rapids',
		'state'='Michigan',
		'postalcode'='49546',
		'country'='USA'
	},
	'phone'={
		'home'='616-123-4567',
		'work'='616-321-7654',
		'mobile'='616-987-6543'
	}
	}).create();

Once we've created the document, it automatically becomes the Active Entity.

var isLoaded=person.loaded(); //will return true	

There is a special _id value that is created by MongoDB when the document is inserted. This serves as your "primary key" provides the unique identifier for the document (when you query for it directly, Mongo is really, really fast):

var pkey=person.get_id();

You may add human readable unique values (tags/slugs) and index them:

property name="tag" schema=true index=true unique=true;
Clone this wiki locally