mongodb语法

use 选择/创建数据库

use 如果数据库不存在则创建

use databases

create 创建集合

db.createCollection("demo")

drop 删除集合

db.demo.drop()

find 查找

find、findOne

db.demo.find()
db.demo.findOne()

ObjectId

db.demo.find({"_id": ObjectId("5f0183767deef9bead24c180")})

findOne

db.demo.findOne({"age": 22})

limit、skip、sort

db.demo.find({"age": {"$gte": 20}}).limit(3).skip(1).sort({"age": 1})

in

db.demo.find({"age": {$in: [18, 20]}})

not in

db.demo.find({"age": {$nin: [18, 20]}})

条件操作符 gt、gte、lt、lte、ne、eq
gt >
gte >=
lt <
lte <=
ne !=
eq =

db.demo.find({"age": {$gt: 20}})
db.demo.find({"age": {$gte: 20}})
db.demo.find({"age": {$lt: 20}})
db.demo.find({"age": {$lte: 20}})
db.demo.find({"age": {$ne: 20}})
db.demo.find({"age": {$eq: 20}})

or

db.demo.find({$or: [{"age": 18}, {"age": 22}]})

正则表达式

db.demo.find({"email": {$regex: "u"}})
db.demo.find({"email": /z/})

原子操作

db.demo.findAndModify({
    "query": {
            "_id": ObjectId("5f01851a7deef9bead24c19f"),
        },
    "update": {
            $set: {
                "age": 29
                }
        }
})
db.demo.findAndModify({
    "query": {
            "_id": ObjectId("5f0185217deef9bead24c1d2"),
        },
    "update": {
            $set: {
                "age": 30
                }
        },
    "new": true
})

explain 查询分析

db.demo.find().explain()

insert添加文档

db.demo.insert({
    "uid" : 8,
    "name" : "华佗",
    "age" : 29,
    "sex" : "男",
    "email" : "[email protected]",
    "mobile" : "13366666666"
})
db.demo.insert([
    {
        "uid" : 6,
        "name" : "法正",
        "age" : 29.0,
        "sex" : "男",
        "email" : "[email protected]",
        "mobile" : "13366666666"
    },
    {
        "uid" : 7,
        "name" : "荀彧",
        "age" : 29.0,
        "sex" : "男",
        "email" : "[email protected]",
        "mobile" : "13366666666"
    }
])
db.demo.insertOne({
    "uid" : 8,
    "name" : "华佗",
    "age" : 29,
    "sex" : "男",
    "email" : "[email protected]",
    "mobile" : "13366666666"
})
db.demo.insertMany([
    {
        "uid" : 6,
        "name" : "法正",
        "age" : 29.0,
        "sex" : "男",
        "email" : "[email protected]",
        "mobile" : "13366666666"
    },
    {
        "uid" : 7,
        "name" : "荀彧",
        "age" : 29.0,
        "sex" : "男",
        "email" : "[email protected]",
        "mobile" : "13366666666"
    }
])

update 更新文档

upsert:可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi:可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。

db.demo.update({"age": 16}, {"$set": {"age": 18}}, {"upsert": true, "multi": true})
db.demo.update({"age": 18}, {"$set": {"age": 16}}, false, false)

inc 参数为1时自加1,为-1时自减1

db.demo.update({"sex": "男"}, {$inc: {"age": 1}})
db.demo.update({"sex": "男"}, {$inc: {"age": 1}}, {"upsert": true, "multi": true})
db.demo.update({"sex": "男"}, {$inc: {"age": -1}})
db.demo.update({"sex": "男"}, {$inc: {"age": -1}}, {"upsert": true, "multi": true})

remove 删除文档

justOne:可选,如果设为 true 或 1,则只删除一个文档,如果不设置该参数,或使用默认值 false,则删除所有匹配条件的文档。

db.demo.remove({"age": 18}, {"justOne": true})

delete 这个是官方推荐的方法deleteMany删除全部文档、deleteOne删除一条文档

db.demo.deleteMany({})
db.demo.deleteOne({})

index索引

创建索引

db.demo.ensureIndex({"age": 1}, {"background": true, "unique": false, "name": "age", "dropDups": false, "v": 1, "weights": 1})

获取全部索引

db.demo.getIndexes()

获取索引大小

db.demo.totalIndexSize()

删除一个索引

db.demo.dropIndex("age")

删除全部索引

db.demo.dropIndexes()

聚合

计算总和

db.demo.aggregate([{$group : {_id : "$name", num_tutorial : {$sum : "$age"}}}])

计算平均值

db.demo.aggregate([{$group : {_id : "$name", num_tutorial : {$avg : "$age"}}}])

获取集合中所有文档对应值得最小值

db.demo.aggregate([{$group : {_id : "$name", num_tutorial : {$min : "$age"}}}])

获取集合中所有文档对应值得最大值

db.demo.aggregate([{$group : {_id : "$name", num_tutorial : {$max : "$age"}}}])

自增

创建sequence、goods集合

db.createCollection("sequence")
db.sequence.insert({_id: "goods_id", sequence_value: 0})
db.createCollection("goods")

添加函数,通过原子操作获取sequence_value自增后的值

db.system.js.insert({
	"_id": "getNextSequenceValue",
	"value": function (sequenceName) {
		var sequenceDocument = db.sequence.findAndModify(
	      {
	         "query": {_id: sequenceName },
	         "update": {$inc: {sequence_value: 1}},
	         "new": true
	      });
	   return sequenceDocument.sequence_value;
	}
})

加载刚刚保存的脚本

db.loadServerScripts()

添加数据

db.goods.insertMany([
    {
        _id: getNextSequenceValue("goods_id"),
        "title": "手机"
    },
    {
    	_id: getNextSequenceValue("goods_id"),
        "title": "台式电脑"
    },
    {
    	_id: getNextSequenceValue("goods_id"),
        "title": "平板电脑"
    },
    {
    	_id: getNextSequenceValue("goods_id"),
        "title": "台灯"
    },
    {
    	_id: getNextSequenceValue("goods_id"),
        "title": "手表"
    }
])

你可能感兴趣的:(mongodb)