MongoDB基本操作

1 创建数据库
use 数据库名称
如果数据库不存在,则创建数据库,如果存在切换到指定数据库

> use runoob
switched to db runoob
> db
runoob

show dbs可以查看所有数据库
如果数据库中没有值,那么就show dbs就列不出来,因此需要在空数据库中插入数据,才会显示出来
MongoDB 中默认的数据库为 test,如果你没有创建新的数据库,集合将存放在 test 数据库中

2 删除数据库
进入数据库 use ceshi0820
然后删除

> db.dropDatabase()
{ "ok" : 1 }

3 创建集合
MongoDB 中使用 createCollection() 方法来创建集合
db.createCollection(name, options)
name: 要创建的集合名称
options: 可选参数, 指定有关内存大小及索引的选项

> db.createCollection("one")
{ "ok" : 1 }

如果要查看已有集合,可以使用show collections

> show collections
one

在 MongoDB 中,你不需要创建集合。当你插入一些文档时,MongoDB 会自动创建集合

> db.mycol2.insert({"name" : "教程"})
> show collections
mycol2

4 删除集合
MongoDB 中使用 drop() 方法来删除集合
db.集合的名字.drop()

> db.one.drop()
true

5 插入文档
文档的数据结构和JSON基本一样
所有存储在集合中的数据都是BSON格式
SON是一种类json的一种二进制形式的存储格式,简称Binary JSON
MongoDB 使用 insert() 或 save() 方法向集合中插入文档,语法如下:
db.集合名字.insert(document)
如果集合不存在,数据库会自动创建

> db.one.insert({title: 'MongoDB 教程',description: 'MongoDB 是一个 Nosql 数据库',tags: ['mongodb', 'database', 'NoSQL']})
WriteResult({ "nInserted" : 1 })

 6 更新文档

db.collection(集合名).update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)

参数说明:
query : update的查询条件,类似sql update查询内where后面的
update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
writeConcern :可选,抛出异常的级别。

举例

db.one.insert({title:'tt',name:'aa',note1:'uu',note2:'rr'})
WriteResult({ "nInserted" : 1 })
> db.one.update({title:'tt',name:'aa'},{$set:{note1:'bb',note2:'rrr'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

 7 删除文档

db.collection(集合名).remove(
   <query>,
   {
     justOne: <boolean>,
     writeConcern: <document>
   }
)

参数说明:
query :(可选)删除的文档的条件。
justOne : (可选)如果设为 true 或 1,则只删除一个文档。
writeConcern :(可选)抛出异常的级别。

> db.one.remove({'title':'MongoDB 教程'})
WriteResult({ "nRemoved" : 1 })

删除集合中所有元素:
db.col.remove({})

8 查询文档

查询文档使用 find() 方法
db.collection.find(query, projection)
query :可选,使用查询操作符指定查询条件
projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略),如果有10个字段,可以指定返回哪几个字段。

db.jihe01.find()
{ "_id" : ObjectId("5b7cc5836b2b7b1f342ce41a"), "par" : "1" }
{ "_id" : ObjectId("5b7cc60b6b2b7b1f342ce41d"), "par" : "1" }
{ "_id" : ObjectId("5b7cc6116b2b7b1f342ce41f"), "par" : "1" }
{ "_id" : ObjectId("5b7ccaf46b2b7b24c08bdad7"), "par" : "2" }
{ "_id" : ObjectId("5b7d28886b2b7b20548aa4d7"), "name" : "张三", "age" : NumberLong(10) }
{ "_id" : ObjectId("5b7d28886b2b7b20548aa4d8"), "name" : "李四", "age" : NumberLong(11) }
{ "_id" : ObjectId("5b7e11476b2b7b14588f623e"), "name" : "张三", "age" : NumberLong(10) }
{ "_id" : ObjectId("5b7e11476b2b7b14588f623f"), "name" : "李四", "age" : NumberLong(11) }
{ "_id" : ObjectId("5b7e15486b2b7b14588f6242"), "name" : "张三", "age" : NumberLong(10) }
{ "_id" : ObjectId("5b7e15486b2b7b14588f6243"), "name" : "李四", "age" : NumberLong(11) }
{ "_id" : ObjectId("5b7e17b86b2b7b14588f6245"), "name" : "张三" }
{ "_id" : ObjectId("5b7e17b86b2b7b14588f6246"), "name" : "李四", "age" : NumberLong(11) }
{ "_id" : ObjectId("5b7e18426b2b7b14588f6248"), "name" : "张三", "age" : null }
{ "_id" : ObjectId("5b7e18426b2b7b14588f6249"), "name" : "李四", "age" : NumberLong(11) }
{ "_id" : ObjectId("5b7e1e9e6b2b7b14588f624c"), "name" : "张三", "age" : null }
{ "_id" : ObjectId("5b7e1e9e6b2b7b14588f624d"), "name" : "李四", "age" : NumberLong(11) }
>

如果你需要以易读的方式来读取数据,可以使用 pretty() 方法,语法格式如下:
db.col.find().pretty()

db.col.find().pretty()

> db.jihe01.find().pretty()
{ "_id" : ObjectId("5b7cc5836b2b7b1f342ce41a"), "par" : "1" }
{ "_id" : ObjectId("5b7cc60b6b2b7b1f342ce41d"), "par" : "1" }
{ "_id" : ObjectId("5b7cc6116b2b7b1f342ce41f"), "par" : "1" }
{ "_id" : ObjectId("5b7ccaf46b2b7b24c08bdad7"), "par" : "2" }
{
        "_id" : ObjectId("5b7d28886b2b7b20548aa4d7"),
        "name" : "张三",
        "age" : NumberLong(10)
}
{
        "_id" : ObjectId("5b7d28886b2b7b20548aa4d8"),
        "name" : "李四",
        "age" : NumberLong(11)
}
{
        "_id" : ObjectId("5b7e11476b2b7b14588f623e"),
        "name" : "张三",
        "age" : NumberLong(10)
}
{
        "_id" : ObjectId("5b7e11476b2b7b14588f623f"),
        "name" : "李四",
        "age" : NumberLong(11)
}
{
        "_id" : ObjectId("5b7e15486b2b7b14588f6242"),
        "name" : "张三",
        "age" : NumberLong(10)
}
{
        "_id" : ObjectId("5b7e15486b2b7b14588f6243"),
        "name" : "李四",
        "age" : NumberLong(11)
}
{ "_id" : ObjectId("5b7e17b86b2b7b14588f6245"), "name" : "张三" }
{
        "_id" : ObjectId("5b7e17b86b2b7b14588f6246"),
        "name" : "李四",
        "age" : NumberLong(11)
}
{
        "_id" : ObjectId("5b7e18426b2b7b14588f6248"),
        "name" : "张三",
        "age" : null
}
{
        "_id" : ObjectId("5b7e18426b2b7b14588f6249"),
        "name" : "李四",
        "age" : NumberLong(11)
}
{
        "_id" : ObjectId("5b7e1e9e6b2b7b14588f624c"),
        "name" : "张三",
        "age" : null
}
{
        "_id" : ObjectId("5b7e1e9e6b2b7b14588f624d"),
        "name" : "李四",
        "age" : NumberLong(11)
}
>

 8.1查询条件的使用

MongoDB 等于
db.col.find({key1:value1}).pretty()
相当于sql: where key1 = value1
例子:在集合jihe01中查询userName等于11的数据

db.jihe01.find({userName:"11"})
{ "_id" : NumberLong(2), "_class" : "com.cfj.ceshi.entity.UserEntity", "userName" : "11", "passWord" : "31" }

MongoDB AND条件
MongoDB 的 find() 方法可以传入多个键(key),每个键(key)以逗号隔开,即常规 SQL 的 AND 条件。
语法格式如下:
db.col.find({key1:value1, key2:value2}).pretty()
例子:在集合jihe01中查询userName等于11的和passWord等于31的数据

 db.jihe01.find({userName:"11",passWord:"31"})
{ "_id" : NumberLong(2), "_class" : "com.cfj.ceshi.entity.UserEntity", "userName" : "11", "passWord" : "31" }

MongoDB OR条件
MongoDB OR 条件语句使用了关键字 $or,语法格式如下:
db.col.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
例子:在集合jihe01中查询userName等于11的或者userName等于小明的数据

db.jihe01.find({$or:[{userName:"11"},{userName:"小明"}]})
{ "_id" : NumberLong(2), "_class" : "com.cfj.ceshi.entity.UserEntity", "userName" : "11", "passWord" : "31" }
{ "_id" : NumberLong(3), "_class" : "com.cfj.ceshi.entity.UserEntity", "userName" : "小明", "passWord" : "fffooo123" }

补充:db.collection.find(query, projection)中projection的使用,指定某些字段在查询结果中不显示出来

例子:查询结果中只显示userName

db.jihe01.find({$or:[{userName:"11"},{userName:"小明"}]},{userName:1})
{ "_id" : NumberLong(2), "userName" : "11" }

_id 键默认返回,需要主动指定 _id:0 才会隐藏

操作符
(>) 大于 - $gt
(<) 小于 - $lt
(>=) 大于等于 - $gte
(<= ) 小于等于 - $lte

举例:在集合etlCheckInfo中查找checkId大于2的数据

db.etlCheckInfo.find({checkId:{$gt:2}})
{ "_id" : ObjectId("5b960e2f3d29f61fe28452da"), "checkId" : NumberLong(3), "readNum" : "0.0", "volume" : 0, "weight" : 0 }
{ "_id" : ObjectId("5b960e2f3d29f61fe28452dc"), "checkId" : NumberLong(3), "readNum" : "25.0", "volume" : 14, "weight" : 6 }
{ "_id" : ObjectId("5b960e2f3d29f61fe28452de"), "checkId" : NumberLong(3), "readNum" : "50.0", "volume" : 52, "weight" : 24 }
{ "_id" : ObjectId("5b960e2f3d29f61fe28452e0"), "checkId" : NumberLong(3), "readNum" : "75.0", "volume" : 111, "weight" : 52 }
{ "_id" : ObjectId("5b960e2f3d29f61fe28452e2"), "checkId" : NumberLong(3), "readNum" : "100.0", "volume" : 187, "weight" : 88 }

排序
在 MongoDB 中使用 sort() 方法对数据进行排序,sort() 方法可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而 -1 是用于降序排列。
db.COLLECTION_NAME.find().sort({KEY:1})
例子:etlLiquid集合中按照collect_value降序,按照collect_date升序

db.etlLiquid.find().sort({collect_value:-1,collect_date:1})

limit() 方法
如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的记录条数。
db.COLLECTION_NAME.find().limit(NUMBER)
举例:排序之后,只取前2行

db.etlLiquid.find().sort({collect_value:-1,collect_date:1}).limit(2)
原文地址:https://www.cnblogs.com/kxm87/p/9518098.html