1 2 3 4 5 6 7 8 9 10 11 import mongoose from 'mongoose' const db = mongoose.connect(MONGODB_URI)db.connection.on('open' , () => { console .log('connected' ) }) db.connection.on('error' , (err ) => { console .log(`db error: ${err} ` ) process.exit })
Schema Schema defines the skeleton of minimal unit
1 2 3 4 5 6 7 import mongoose from 'mongoose' const TestSchema = new mongoose.Schema({ name: { type : String }, age: { type : Number , default : 0 }, time: { type : Date , default : Date .now() }, email: { type : String , default : '' }, })
Primitive Types: String, Number, Boolean, null, Array, Document, Date
Model Instance of Schema, it has ability to operate db
1 2 const db = mongoose.connect(MONGODB.URI)const testModal = db.model('test1' , TestSchema)
test1: Name of Collection in the DB
Entity Instance of Model, it has ability to operate db
1 2 3 4 5 6 7 8 const TestEntity = new TestModel({ name: 'Lenka' , age: 35 , email: '[email protected] ' , }) console .log(TestEntity.name) console .log(TestEntity.age)
Collection 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import mongoose from 'mongoose' const db = mongoose.connect(MONGODB.URI)const TestSchema = new mongoose.Schema({ name: { type : String }, age: { type : Number , default : 0 }, email: { type : String }, time: { type : Date , default : Date .now() }, }) const TestModel = db.model('test1' , TestSchema)const TestEntity = new TestModel({ name: 'hello world' , age: 12 , email: '[email protected] ' , }) TestEntity.save((err, doc ) => { if (err) => { console .log('error: ' + err) } else { console .log(doc) } })
Find 1 obj.find(condition, field, callback)
if fields
is omited, or null
, the docs will return all attributes
1 2 3 4 5 6 7 8 Model.find({ 'age' : 25 }, (err, docs ) => { if (err) return console .log(err) console .log(docs) }) Model.find({}, { name : 1 , age : 1 , _id : 0 }, (err, docs ) => { })
findOne Same as find
, except it will return only one matched
1 2 3 TestModel.findOne(condition, fields, (err, doc ) => { })
findById Same as findOne
, but it only find by _id
1 2 3 TestModel.findById('obj._id' , (err, doc ) => { })
Create 1 Model.create({}, callback)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Model.create({ name: 'test' , age: 12 , }, (err, doc ) => { if (err) return console .log(err) console .log(doc) }) TestModel.create([ { name : 'test1' , age : 20 }, { name : 'test2' , age : 20 }, { name : 'test3' , age : 20 }, { name : 'test4' , age : 20 }, { name : 'test5' , age : 20 }, { name : 'test6' , age : 20 }, { name : 'test7' , age : 20 }, { name : 'test8' , age : 20 }, { name : 'test9' , age : 20 }, ])
Save 1 Entity.save({}, callback)
1 2 3 4 5 6 7 8 const Entity = new Model({ name: 'entity_save' }) Entity.save((err, doc ) => { if (err) return console .log(err) console .log(doc) })
Model.create
Entity.save
Update 1 obj.update({}, callback)
1 2 3 4 5 6 7 8 const conditions = { name : 'test_upate' }const update = { $set : { age : 16 } }TestModel.update(conditions, update, (err ) => { if (err) return console .log(err) console .log('success' ) })
Remove 1 obj.remove({}, callback)
1 2 3 4 5 6 const conditions = { name : 'tim' }TestModel.remove(conditions, (err ) => { if (err) return console .log(err) console .log('success' ) })
Advanced Search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Model.find({ "age" : { "$gt" : 18 , "$lt" : 50 } }, (err, docs ) => { }) Model.find({ "age" : { "$in" : [20 , 30 ] } }, (err, docs ) => { }) Model.find({ "$or" : [ { "name" : "yaya" }, { "age" : 28 }, ] }, (err, docs ) => { }) Model.find({ name: "$exists" }, (err, docs ) => { })
Limit 1 2 3 find(condition, { limit : 20 }, function (err, docs ) => { })
1 2 3 Model.find({}, null , { limit : 20 }, (err, docs ) => { })
Skip Skim first n docs
1 2 3 find({}, null , { skip : 4 }, (err, docs ) => { })
if total docs less than 4, it will output nothing
Sort -1
: descending, 1
: ascending
1 2 3 Model.find({}, null , { sort : { age : -1 } }, (err, docs ) => { })
ObjectId The default id _id
could be any types in mongodb, and default to be ObjectId
ObjectId, is a 12-types BSON String.
Schema add Attribute 1 2 3 4 5 6 7 import mongoose from 'mongoose' const TestSchema = new mongoose.SchemaTestSchema.add({ name: 'String' , email: 'String' , age: 'Number' , })
Schema add instance method 1 2 3 4 5 6 7 8 9 10 11 import mongoose from 'mongoose' const TextSchema = new mongoose.Schema({ name: 'String' , }) TestSchema.method('test' , () => { console .log('hah' ) }) const say = mongoose.model('say' , TestSchema)var lenka = say()lenka.say()
Schema add static method
TestSchema.static('findByName', (name, cb) => { return this.find({name: name}, cb) })
const TestModel = db.model('test', TestSchema) TestModel.findByName('tim', (err, docs) => { // ... })