p-odm

ODM mongodb library for node.js

ODM

ODM is a new, innovative and easy way to use MongoDB documents, as Models, in your code. It uses the JSON schema standard for validating the documents.

odm.connect('mongodb://127.0.0.1:27017/simple');
var Person = odm.model("persons", {
  "type" : "object",
  "properties": {
    "name": {"type": "string"},
  }
});

We embed an address model in person:

// Address, to be embedded on Person
var Address = odm.model({
  "id": "Simple#Address",
  "type" : "object",
  "properties": {
    "lines": {
      "type": "array",
      "items": {"type": "string"}
    },
    "zip": {"type": "string"},
    "city": {"type": "string"},
    "country": {"type": "string"}
  }
});

The changed person model:

var Person = odm.model("persons", {
  "type" : "object",
  "properties": {
    "name": {"type": "string"},
    "address": {"$ref": "Simple#Address"}
  }
});

Parses a JSON string to a JSON document. It is aware of ISO Dates and ObjectIds and coverts them on the fly.

Finds one document or fields, satisfying query.

Person.findOne({'name': 'Barack Obama'}, function (error, document) {
  if (error) {
    console.log("error", error);
  }
  console.log(document);
});

Finds one document by id, returining fields.

Person.findById("4ff3fcf14335e9d6ba000001", function (error, document) {
  if (error) {
    console.log("error", error);
  }
  console.log(document);
});

Finds all documents or fields, satisfying query.

Person.find({'name': 'Barack Obama'}, function (error, documents) {
  if (error) {
    console.log("error", error);
  }
  console.log(documents);
});

Finds all documents or fields.

Person.findAll(function (error, documents) {
  if (error) {
    console.log("error", error);
  }
  console.log(documents);
});

Removes all documents satisfying query.

Update all documents satisfying query, with document.

Loads one Id or array of ids, it is similar to a simple find, however the number of results and order is the same as the array argument

Adds an index and will also add a findByXXX method, where XXX is the name of the fieldOrSpec

Returns true or false, or all errors in case verbose is true.

Saves the instance model.

p.save(function (error, id) {
  if (error) {
    console.log("error", error);
  } 
  console.log(id);
});

Update the instance model.

Insert the instance model.

Remove the instance model.