查詢基礎(chǔ)
1、查詢方法
mongoose查詢使用最基礎(chǔ)的方法就是find、findOne方法,前者查詢所有滿足條件的值,后者取滿足條件的某一個值。
2、查詢條件
mongoose查詢條件其實就是在find方法的基礎(chǔ)上添加mongodb條件操作符,如Thing.find().gt('age', 21)就等同于Thing.find({age: {$gt: 21}}),mongodb條件操作符如下:
$or 或關(guān)系
$nor 或關(guān)系取反
$gt 大于
$gte 大于等于
$lt 小于
$lte 小于等于
$ne 不等于
$in 在多個值范圍內(nèi)
$nin 不在多個值范圍內(nèi)
$all 匹配數(shù)組中多個值
$regex 正則,用于模糊查詢
$size 匹配數(shù)組大小
$maxDistance 范圍查詢,距離(基于LBS)
$mod 取模運算
$near 鄰域查詢,查詢附近的位置(基于LBS)
$exists 字段是否存在
$elemMatch 匹配內(nèi)數(shù)組內(nèi)的元素
$within 范圍查詢(基于LBS)
$box 范圍查詢,矩形范圍(基于LBS)
$center 范圍醒詢,圓形范圍(基于LBS)
$centerSphere 范圍查詢,球形范圍(基于LBS)
$slice 查詢字段集合中的元素(比如從第幾個之后,第N到第M個元素)
3、填充對象
查詢對象時,對象中存在其他對象的引用,查詢出來的引用對象默認(rèn)是顯示引用對象的id,如果需要引用對象的其他屬性就需要使用populate方法填充引用對象。
如果對以上知識點不太了解可以參考:
查詢實例
schema.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name : { type: String, unique: true },
posts : [{ type: Schema.Types.ObjectId, ref: 'Post' }]
});
var User = mongoose.model('User', UserSchema);
var PostSchema = new Schema({
poster : { type: Schema.Types.ObjectId, ref: 'User' },
comments : [{ type: Schema.Types.ObjectId, ref: 'Comment' }],
title : String,
content : String
});
var Post = mongoose.model('Post', PostSchema);
var CommentSchema = new Schema({
post : { type: Schema.Types.ObjectId, ref: "Post" },
commenter : { type: Schema.Types.ObjectId, ref: 'User' },
content : {
main: String,
label: String
},
points: [
point: [{type: Schema.Types.ObjectId, ref: 'Point'}]
]
});
var Comment = mongoose.model('Comment', CommentSchema);
var PointSchema = new mongoose.Schema({
name: String,
parent: {type: Schema.Types.ObjectId, ref: 'point'},
children: [{type: Schema.Types.ObjectId, ref: 'point'}]
})
var Point = mongoose.model('Point', PointSchema);
1、深層屬性查詢
有些對象結(jié)構(gòu)比較復(fù)雜,屬性可能存在多層嵌套關(guān)系,有時需要通過對象屬性下屬的屬性查詢對象,如通過content的label的值查詢Comment
Comment.find({'content.label': value}, function (err, comment) {
console.log(comment)
})
2、二維數(shù)組查詢
如果二維數(shù)組結(jié)構(gòu)為[[]],這樣的數(shù)組是可以查詢,但是填充數(shù)組里對象時會有問題
Comment.find({'points': value}).populate('points').exec(function (err, comment) {
console.log(comment) // 無法填充points
})
所以需要填充二維數(shù)組里的對象時,不能使用這種結(jié)構(gòu),而應(yīng)該如schema.js中一樣,將里面的數(shù)組先作為對象保存
Comment.find({'points': value}).populate('points.point').exec(function (err, comment) {
console.log(comment) // 無法填充points
})
3、循環(huán)填充
結(jié)構(gòu)如Point,讀取point時,需要填充children,而childern的childern也需要填充,使用populate只能填充當(dāng)前的childern,在schema.js添加:
PointSchema.pre('find', function(next) {
this.populate('children')
next()
})
這樣每次查詢時,自動為point填充childern
4、多表聯(lián)合查詢
mongoose其實沒有多表聯(lián)合查詢的方法,不過我們可以通過多次查詢來實現(xiàn)。
通過user的name、post的content查詢post:
User.find({name: name}, function (err, users) {
Post.find({poster: {$in: users}, content: content}, function (err, posts) {
console.log(posts)
})
})
有時我們也需要對取出來的數(shù)據(jù)進(jìn)行再次過濾,而不是通過查詢語句查詢
通過user的name、post的content、comment的content.main查詢post:
User.find({name: name}, function (err, users) {
Post.find({poster: {$in: users}, content: content}).populate('commenter').exec(function (err, posts) {
posts.filter(function(post) {
return post.commenter.content.main === value
})
})
})