声明:本文乃“运维家”原创,转载请注明出处,更多内容请关注公众号“运维家”。

主旨
环境
mongodb数据库
PS:如果没有搭建的可以看下前两天发布的部署教程。
连接数据库
语法:mongo IP:Port实例:mongo 192.168.112.130:27017
查看数据库
语法:show dbs # 查看所有数据库db # 查看当前位于哪个数据库中实例:testrs:PRIMARY> show dbsadmin 0.000GBlocal 0.000GBtestrs:PRIMARY>testrs:PRIMARY> dbadmintestrs:PRIMARY>
创建(切换)数据库
语法:use database_name实例:testrs:PRIMARY> use yunweijia # 创建数据库yunweijiaswitched to db yunweijiatestrs:PRIMARY> db # 查看当前位于哪个数据库中yunweijiatestrs:PRIMARY>testrs:PRIMARY> show dbs # 查看所有数据库admin 0.000GBlocal 0.000GBtestrs:PRIMARY>
testrs:PRIMARY> db.yunweijia.insert({"name":"运维家"}) # 插入WriteResult({ "nInserted" : 1 })testrs:PRIMARY> show dbs # 再次查看,就发现新创建的数据库显示出来了admin 0.000GBlocal 0.000GByunweijia 0.000GBtestrs:PRIMARY>
删除数据库
语法:# 删除当前数据库,执行该命令的时候一定要注意自己在哪个数据库中db.dropDatabase()实例:testrs:PRIMARY> show dbsadmin 0.000GBlocal 0.000GByunweijia 0.000GBtestrs:PRIMARY> use yunweijiaswitched to db yunweijiatestrs:PRIMARY> db.dropDatabase(){ "dropped" : "yunweijia", "ok" : 1 }testrs:PRIMARY> show dbsadmin 0.000GBlocal 0.000GBtestrs:PRIMARY>
创建集合
语法:db.createCollection(name, options)name: 要创建的集合名称options: 可选参数, 指定有关内存大小及索引的选项capped:(可选)如果为 true,则创建固定集合。固定集合是指有着固定大小的集合,当达到最大值时,它会自动覆盖最早的文档。当该值为 true 时,必须指定 size 参数。size:(可选)为固定集合指定一个最大值,即字节数。如果 capped 为 true,也需要指定该字段。max:(可选)指定固定集合中包含文档的最大数量。实例:testrs:PRIMARY> use yunweijia;switched to db yunweijiatestrs:PRIMARY> db.createCollection("ceshi"){ "ok" : 1 }testrs:PRIMARY> show collections # 查看集合ceshitestrs:PRIMARY>
testrs:PRIMARY> db.yunweijia.insert({"name":"运维家"})WriteResult({ "nInserted" : 1 })testrs:PRIMARY>testrs:PRIMARY> show collectionsceshiyunweijiatestrs:PRIMARY>
删除集合
语法:db.collection.drop()实例:testrs:PRIMARY> use yunweijia # 切换数据库到yunweijiaswitched to db yunweijiatestrs:PRIMARY> show collections # 查看所有集合ceshijiersuneryunweijiatestrs:PRIMARY> db.jier.drop() # 删除jier集合truetestrs:PRIMARY> show collections # 查看所有集合,发现jier集合被删除了ceshisuneryunweijiatestrs:PRIMARY>
插入文档
语法:# 若插入的数据主键已经存在,则会抛 org.springframework.dao.DuplicateKeyException 异常,提示主键重复,不保存当前数据。db.COLLECTION_NAME.insert(document)# 如果 _id 主键存在则更新数据,如果不存在就插入数据。db.collection.insertOne()格式为:db.collection.insertOne(<document>,{writeConcern: <document>})# 如果 _id 主键存在则更新数据,如果不存在就插入数据。db.collection.replaceOne()# 向一个集合中插入多个文档db.collection.insertMany()格式:db.collection.insertMany([ <document 1> , <document 2>, ... ],{writeConcern: <document>,ordered: <boolean>})参数说明:document:要写入的文档。writeConcern:写入策略,默认为 1,即要求确认写操作,0 是不要求。ordered:指定是否按顺序写入,默认 true,按顺序写入。
实例1如下:
testrs:PRIMARY> db.col.insert({title: 'MongoDB 基础操作',description: 'MongoDB 是一个 Nosql 数据库',by: '运维家',url: 'http://www.baidu.com',tags: ['mongodb', 'database', 'NoSQL'],likes: 100})WriteResult({ "nInserted" : 1 })testrs:PRIMARY># 以上实例中 col 是我们的集合名,如果该集合不在该数据库中, MongoDB 会自动创建该集合并插入文档。# 查看已插入文档testrs:PRIMARY> db.col.find(){ "_id" : ObjectId("62011ae10e4263c83bd0dec8"), "title" : "MongoDB 基础操作", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "运维家", "url" : "http://www.baidu.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }testrs:PRIMARY>
实例2如下:
先将数据定义为一个变量,然后执行插入操作。
testrs:PRIMARY> document=({title: 'MongoDB 基础操作',description: 'MongoDB 是一个 Nosql 数据库',by: '运维家',url: 'http://www.baidu.com',tags: ['mongodb', 'database', 'NoSQL'],likes: 100});# 结果显示如下{"title" : "MongoDB 基础操作","description" : "MongoDB 是一个 Nosql 数据库","by" : "运维家","url" : "http://www.baidu.com","tags" : ["mongodb","database","NoSQL"],"likes" : 100}# 执行插入操作testrs:PRIMARY> db.col.insert(document)WriteResult({ "nInserted" : 1 })testrs:PRIMARY> db.col.find(){ "_id" : ObjectId("62011ae10e4263c83bd0dec8"), "title" : "MongoDB 基础操作", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "运维家", "url" : "http://www.baidu.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }{ "_id" : ObjectId("62011c0f0e4263c83bd0dec9"), "title" : "MongoDB 基础操作", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "运维家", "url" : "http://www.baidu.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }testrs:PRIMARY>
更新文档
语法: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 :可选,抛出异常的级别。实例:testrs:PRIMARY> db.col.update({'title':'MongoDB 基础操作'},{$set:{'title':'MongoDB数据库'}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })testrs:PRIMARY> db.col.find().pretty(){"_id" : ObjectId("62011ae10e4263c83bd0dec8"),"title" : "MongoDB数据库","description" : "MongoDB 是一个 Nosql 数据库","by" : "运维家","url" : "http://www.baidu.com","tags" : ["mongodb","database","NoSQL"],"likes" : 100}{"_id" : ObjectId("62011c0f0e4263c83bd0dec9"),"title" : "MongoDB 基础操作","description" : "MongoDB 是一个 Nosql 数据库","by" : "运维家","url" : "http://www.baidu.com","tags" : ["mongodb","database","NoSQL"],"likes" : 100}testrs:PRIMARY
db.col.update({'title':'MongoDB 基础操作'},{$set:{'title':'MongoDB数据库'}},{multi:true})
删除文档
语法:db.collection.remove(<query>,{justOne: <boolean>,writeConcern: <document>})参数解释:query :(可选)删除的文档的条件。justOne : (可选)如果设为 true 或 1,则只删除一个文档,如果不设置该参数,或使用默认值 false,则删除所有匹配条件的文档。writeConcern :(可选)抛出异常的级别。
实例如下:
# 新插入一条数据,用来比较testrs:PRIMARY> db.col.insert({title: 'MongoDB 基础操作',... description: 'MongoDB 是一个 Nosql 数据库',... by: '运维家',... url: 'http://www.baidu.com',... tags: ['mongodb', 'database', 'NoSQL'],... likes: 100... })WriteResult({ "nInserted" : 1 })testrs:PRIMARY> db.col.find() # 可以看到有三条,其中两条数据title一样{ "_id" : ObjectId("62011ae10e4263c83bd0dec8"), "title" : "MongoDB数据库", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "运维家", "url" : "http://www.baidu.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }{ "_id" : ObjectId("62011c0f0e4263c83bd0dec9"), "title" : "MongoDB 基础操作", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "运维家", "url" : "http://www.baidu.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }{ "_id" : ObjectId("62011e580e4263c83bd0deca"), "title" : "MongoDB 基础操作", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "运维家", "url" : "http://www.baidu.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }testrs:PRIMARY> db.col.remove({'title':'MongoDB 基础操作'}) # 根据title为条件进行删除WriteResult({ "nRemoved" : 2 })testrs:PRIMARY> db.col.find() # 结果是将两条title一致的全部进行删除{ "_id" : ObjectId("62011ae10e4263c83bd0dec8"), "title" : "MongoDB数据库", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "运维家", "url" : "http://www.baidu.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }testrs:PRIMARY>
db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
>db.col.remove({})>db.col.find()
查询文档
语法:db.collection.find(query, projection)参数解释:query :可选,使用查询操作符指定查询条件projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。如果你需要以易读的方式来读取数据,可以使用 pretty() 方法,语法格式如下:db.col.find().pretty()实例:testrs:PRIMARY> db.col.find().pretty(){"_id" : ObjectId("62011ae10e4263c83bd0dec8"),"title" : "MongoDB数据库","description" : "MongoDB 是一个 Nosql 数据库","by" : "运维家","url" : "http://www.baidu.com","tags" : ["mongodb","database","NoSQL"],"likes" : 100}testrs:PRIMARY>
至此,mongodb的基础操作,结束。
文章转载自运维家,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




