Mongoose--Model(增刪查改)

建立Modelhtml

var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var Tank = mongoose.model('Tank', schema);

Adding

var Tank = mongoose.model('Tank', yourSchema);

var small = new Tank({ size: 'small' });
small.save(function (err) {
  if (err) return handleError(err);
  // saved!
})

// or

Tank.create({ size: 'small' }, function (err, small) {
  if (err) return handleError(err);
  // saved!
})

// or

small.save(fn?)

Querying

Finding documents is easy with Mongoose, which supports the rich query syntax of MongoDB. Documents can be retreived using each models findfindByIdfindOne, or where static methods.mongodb

Tank.find({ size: 'small' }).where('createdDate').gt(oneYearAgo).exec(callback);

Removing

Models have a static remove method available for removing all documents matching conditions.api

Tank.remove({ size: 'large' }, function (err) {
  if (err) return handleError(err);
  // removed!
});

Updating

Each model has its own update method for modifying documents in the database without returning them to your application. See the API docs for more detail.app

相關文章
相關標籤/搜索