mongoose使用之查询篇

作者: markdown | 来源:发表于2017-04-19 15:05 被阅读5858次

查询基础

1、查询方法

mongoose查询使用最基础的方法就是find、findOne方法,前者查询所有满足条件的值,后者取满足条件的某一个值。

2、查询条件

mongoose查询条件其实就是在find方法的基础上添加mongodb条件操作符,如Thing.find().gt('age', 21)就等同于Thing.find({age: {$gt: 21}}),mongodb条件操作符如下:

    $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个元素)

3、填充对象

查询对象时,对象中存在其他对象的引用,查询出来的引用对象默认是显示引用对象的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、深层属性查询

有些对象结构比较复杂,属性可能存在多层嵌套关系,有时需要通过对象属性下属的属性查询对象,如通过content的label的值查询Comment

Comment.find({'content.label': value}, function (err, comment) {
    console.log(comment)
})

2、二维数组查询

如果二维数组结构为[[]],这样的数组是可以查询,但是填充数组里对象时会有问题

Comment.find({'points': value}).populate('points').exec(function (err, comment) {
    console.log(comment) // 无法填充points
})

所以需要填充二维数组里的对象时,不能使用这种结构,而应该如schema.js中一样,将里面的数组先作为对象保存

Comment.find({'points': value}).populate('points.point').exec(function (err, comment) {
    console.log(comment) // 无法填充points
})

3、循环填充

结构如Point,读取point时,需要填充children,而childern的childern也需要填充,使用populate只能填充当前的childern,在schema.js添加:

PointSchema.pre('find', function(next) {
  this.populate('children')
  next()
})

这样每次查询时,自动为point填充childern

4、多表联合查询

mongoose其实没有多表联合查询的方法,不过我们可以通过多次查询来实现。
通过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)
    })
})

有时我们也需要对取出来的数据进行再次过滤,而不是通过查询语句查询
通过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
        })
    })
})

相关文章

  • mongoose使用之查询篇

    查询基础 1、查询方法 mongoose查询使用最基础的方法就是find、findOne方法,前者查询所有满足条件...

  • nodejs操作mongodb数据库(mongoose)

    创建项目目录并安装mongodb、mongoose mongoose插入数据 mongoose查询数据 mongo...

  • # nodejs操作mongodb数据库(mongoose)

    创建项目目录并安装mongodb和mongoose(在root下) mongoose插入数据 mongoose查询...

  • Mongoose介绍和入门

    Mongoose介绍和入门 mongoose对查询结果进行排序 mongoose教程06--排序 MyModel....

  • mongoose的简单使用

    npm install mongoose 下载mongoose模块使用之前先打开mongodb数据库mongod...

  • node之mongoose(二)

    Mongoose 常见查询方法 Mongoose库提供了find , findOne , findById 等方法...

  • mongoose学习笔记

    首先是基础crud操作 查询简单查询条件查询 插入 更新 查询 简单查询 mongoose: 条件查询 (>) 大...

  • Node操作mongodb

    一、下载Node的数据库三方包(mongoose) npm install mongoose 二、查询 三、删除 ...

  • mongoose 空间查询

      工作中我用了Koa2做了后台,选用了mongo数据库。因为要用到空间查询显示当前地图视图的空间查询结果,经过一...

  • koa mongoose查询

    一、分页查询+查询所有 二、添加数据到数据库 注:new Article为创建对应的数据模型 三、删除数据 四、修改数据

网友评论

    本文标题:mongoose使用之查询篇

    本文链接:https://www.haomeiwen.com/subject/xcnczttx.html