mongoose使用之查詢篇

查詢基礎(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
        })
    })
})
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,533評論 19 139
  • 原文地址 本文簡單的介紹了數(shù)據(jù)庫,以及如何在 Node/Express 中應(yīng)用他們。之后展示如何使用Mongoos...
    前端幼兒班閱讀 5,302評論 1 5
  • 參考深入淺出mongoose 連接mongoose mongoose連接數(shù)據(jù)庫有兩種方式第一種: 第二種: mon...
    bacbcc94613b閱讀 12,430評論 1 27
  • 2017年10月11日種子日記 何德勝第72天 我今天不是為了我一個人而聞思修行,而是為了一切如母有情眾生,愿他們...
    何德勝覺悟閱讀 340評論 2 3
  • 人工智能不會引發(fā)大面積失業(yè),不過造成小面積的失業(yè)高潮,等到度過之后就會恢復(fù)正常的。 這個問題和兩個世紀(jì)之前,工業(yè)革...
    踏雪飛熊閱讀 564評論 0 0

友情鏈接更多精彩內(nèi)容