Mongoose 基本用法

 

1. SchemaTypes數據類型

數據類型 描述
String 字符串
Number 數字
Date 日期
Boolean 布爾值
Mixed 混合
Objectid 對象ID
Array 數組

 

 

 

 

 

 

 

 

 

 

2. 基本用法

首先建立3張表:segmentfault

var studentSchema = new student({
  name: { // 學生姓名
      type: String,
      required: true
  },
 classNo: {  // 班級編號
      type: String
  },
  createTime: {  // 建立時間
      type: Date,
      default: Date.now
  }
})

var ClassSchema = new classNo({
    className: {  // 班級名稱
        type: String,
        required: true,
        unique: true
    },
    teaNo:{  // 教師編號
        type: String
    },
    createTime: {
        type: Date,
        default: Date.now
    }
})

var TeaSchema = new teacher({
    teaName: {  // 教師名稱
        type: String,
        required: true,
        unique: true
    },
    createTime: {
        type: Date,
        default: Date.now
    }
})

 

  • 查詢所有

function findAll() {
    stu.find(function (err, ret) {
        if (err) {
            console.log('查詢失敗')
        } else {
            console.log(ret)
        }
    })
}
  • 分頁查詢( limit : 須要顯示多少條數據, skip : 跳過多少數據 )

function findAll() {
    stu.find(function (err, ret) {
        if (err) {
            console.log('查詢失敗')
        } else {
            console.log(ret)
        }
    }).limit(2)
    .skip(2)
}
  • 根據時間倒敘查詢 sort

function findAll() {
    stu.find(function (err, ret) {
        if (err) {
            console.log('查詢失敗')
        } else {
            console.log(ret)
        }
    }).sort({createTime: -1})
}
  • 按名字查詢(返回數組)

function findName() {
    stu.find({
        name: '小名'
    }, function (err, ret) {
        console.log(ret)
    })
}


返回值:
[ { _id: 5c0894c217973525c41d0ede,
    name: '小名',
    classNo: '5c08944bfb6d6720cca60214',
    createTime: 2018-12-06T03:17:22.933Z,
    __v: 0 } ]
  • 按名字查詢(返回對象)

function findName() {
    stu.findOne({
        name: '小名'
    }, function (err, ret) {
        console.log(ret)
    })
}


返回值:
{ _id: 5c0894c217973525c41d0ede,
  name: '小名',
  classNo: '5c08944bfb6d6720cca60214',
  createTime: 2018-12-06T03:17:22.933Z,
  __v: 0 }
  • OR 條件查詢

function findAll() {
    stu.find({
        $or: [
            {name: '哎哎哎'},
            {classNo: '5c08944bfb6d6720cca60214'}
        ]
    },function (err, ret) {
        if (err) {
            console.log('查詢失敗')
        } else {
            console.log(ret)
        }
    })
}
  • 模糊查詢 (查詢名字 帶 的數據)

function findAll() {
    stu.find({
        name: /小/
    },function (err, ret) {
        if (err) {
            console.log('查詢失敗')
        } else {
            console.log(ret)
        }
    })
}
  • 找出名字 帶 ,且只輸出 name 字段 【注意】: _id字段默認輸出

function findAll() {
    stu.find({
        name: /小/
    }, 'name' ,function (err, ret) {
        if (err) {
            console.log('查詢失敗')
        } else {
            console.log(ret)
        }
    })
}
  • 不帶 _id ,能夠進行以下設置

function findAll() {
    stu.find({
        name: /小/
    }, {'name': 1, _id: 0} ,function (err, ret) {
        if (err) {
            console.log('查詢失敗')
        } else {
            console.log(ret)
        }
    })
}

 

3. 經常使用查詢條件

$or    或關係
$nor    或關係取反
$gt    大於
$gte    大於等於
$lt    小於
$lte    小於等於
$ne    不等於
$in    在多個值範圍內
$nin    不在多個值範圍內
$all    匹配數組中多個值
$regex   正則,用於模糊查詢
$size   匹配數組大小
$maxDistance 範圍查詢,距離(基於LBS)
$mod    取模運算
$near    鄰域查詢,查詢附近的位置(基於LBS)
$exists   字段是否存在
$elemMatch 匹配內數組內的元素
$within   範圍查詢(基於LBS)
$box     範圍查詢,矩形範圍(基於LBS)
$center   範圍醒詢,圓形範圍(基於LBS)
$centerSphere 範圍查詢,球形範圍(基於LBS)
$slice    查詢字段集合中的元素(好比從第幾個以後,第N到第M個元素

 

 

參考文檔

相關文章
相關標籤/搜索