描述 | |
---|---|
String | 字符串 |
Number | 数字 |
Date | 日期 |
Boolean | 布尔值 |
Mixed | 混合 |
Objectid | 对象ID |
Array | 数组 |
首先创建3张表:
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)}})
}
function findAll() {stu.find(function (err, ret) {if (err) {console.log('查询失败')} else {console.log(ret)}}).limit(2).skip(2)
}
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)}})
}
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)}})
}
$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个元素
-
https://segmentfault.com/a/1190000012095054