mongo CRUD数据操作符汇总

本文详细介绍了MongoDB中常用的操作符、查询语法、数组处理、条件查询、聚合操作及特殊函数的应用,包括等价、大于、小于、不等于、在条件中、不在条件中、逻辑运算符、元素查询、字段存在性、数组筛选、元素匹配、数组大小控制、备注与条件判断等功能。

比较

namedescription
$eq等于
$gt大于
$gte大于等于
$lt小于
$lte小于等于
$ne不等于
$in在条件中
$nin不在条件中

逻辑

namedescription
$or或者
$and
$not
$nor既不也不,nor:[{name:’bugall’,age:12}],名字不等于’bugall’,age不等于12

元素

namedescription
$exists字段是否存在
$type字段的字段类型

数组

namedescription
$all获取所有满足条件的数据
$elemMatch获取所有满足条件的数据,下面有具体讲解
$size如果是数组形式的返回值,定义其返回数组元素个数

备注

namedescription
$comment给命令加上一条描述或是备注

$where

db.myCollection.find( { $where: "this.credits == this.debits" } );
db.myCollection.find( { $where: "obj.credits == obj.debits" } );

db.myCollection.find( { $where: function() { return (this.credits == this.debits) } } );
db.myCollection.find( { $where: function() { return obj.credits == obj.debits; } } );
where接受字符串函数两种形式的参数,其中也提供了一些函数。
assert()
BinData()
DBPointer()
DBRef()
doassert()
emit()
gc()
HexData()
hex_md5()
isNumber()
isObject()
ISODate()
isString()
Map()
MD5()
NumberInt()
NumberLong()
ObjectId()
print()
printjson()
printjsononeline()
sleep()
Timestamp()
tojson()
tojsononeline()
tojsonObject()
UUID()
version()

注意:

因为用了$where后mongo生成的执行计划不能很好的利用索引,所以这里不建议大家使用$where

$all

选择满足条件的所有数据(documents)。
    { <field>: { $all: [ <value1> , <value2> ... ] } }
$all的功能是和$and的功能相同的,例如$and:
    { $and: [ { tags: "ssl" }, { tags: "security" } ] }

$and

    { $and: [ { tags: "ssl" }, { tags: "security" } ] }

$batchSize

db.inventory.find().batchSize(10)
控制返回结果的数据数量(多少条数据),注意,在mongo shell里,batchSize()的设置并不会生效。同时distinct()也不会生效

$box

db.places.find( {
   loc: { $geoWithin: { $box:  [ [ 0, 0 ], [ 100, 100 ] ] } }
} )
定义一个"盒子",2个点可以确定一个"盒子的位置"(我们把盒子理解为二维平面的矩形),那么
上面这句话的意思就是找出在这个"盒子内的点",[22,22],[13,75],[19,24]这些值都将被
返回

$center

    db.places.find(
   { loc: { $geoWithin: { $center: [ [-74, 40.74], 10 ] } } }
)
返回所有在这个平面圆里的点(圆心坐标是(-74,40.74),圆半径是10)

$comment

    db.collection.find( { $query: { <query> }, $comment: <comment> } )
可以理解为为每一次执行加入一条备注,这些备注会被记录在profile里,有助于后期排错

$count

    db.collection.find( { a: 5, b: 5 } ).count()
计数

$distinct

    db.collection.distinct(field, query)
去重

$elemMatch

    { <field>: { $elemMatch: { <query1>, <query2>, ... } } }
    { _id: 1, results: [ 82, 85, 88 ] }
    { _id: 2, results: [ 75, 88, 89 ] }

    db.scores.find(
       { results: { $elemMatch: { $gte: 80, $lt: 85 } } }
    )
满足一区间

$exists

    { field: { $exists: <boolean> } }

    db.records.find( { a: { $exists: true } } )
    result:
        { a: 5, b: 5, c: null }
        { a: 3, b: null, c: 8 }
        { a: null, b: 3, c: 9 }
        { a: 1, b: 2, c: 3 }
        { a: 2, c: 5 }
        { a: 3, b: 2 }
        { a: 4 }

    db.records.find( { b: { $exists: false } } )
    result:
        { a: 2, c: 5 }
        { a: 4 }
        { c: 6 }

$find

    query.find({ name: 'Los Pollos Hermanos' }).find(callback)
查找所有满足条件的数据

$findOne

    query.findOne({ name: 'Los Pollos Hermanos' }).find(callback)
返回一个满足条件的数据

$findAndModify

    {
          findAndModify: <collection-name>,
          query: <document>,
          sort: <document>,
          remove: <boolean>,
          update: <document>,
          new: <boolean>,
          fields: <document>,
          upsert: <boolean>
    }
FieldDescription
query一条普通的请求语句,可能会得到多条数据,但是findAndModify只会处理第一条数据
sort因为findAndModify只能处理一条数据,所以通常我们会把query得到的数据进行排序
remove如果为true则移除query后的第一条数据,默认为false
update更新query后的第一条
new当被设置为true的时候,则返回修改后的数据而不是原始数据,默认为false
fields设置分组
upsert当值为true时,更新的值如果存在则更新,如果不存在则插入。默认为false
db.people.findAndModify( {
   findAndModify: "people",
   query: { name: "Gus", state: "active", rating: 100 },
   sort: { rating: 1 },
   update: { $inc: { score: 1 } },
   upsert: true,
   new : true
} );
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值