博客
关于我
MongoDB数据库操作详解:基础篇
阅读量:797 次
发布时间:2023-02-09

本文共 4773 字,大约阅读时间需要 15 分钟。

MongoDB 常用日常操作命令指南

主旨

本文将介绍MongoDB的一些常用日常操作命令,帮助您快速熟悉和使用MongoDB进行数据管理。

环境

MongoDB数据库

连接数据库

使用MongoDB客户端工具(如mongosh)或命令行工具连接数据库。

语法:

mongo IP:Port

实例:

mongo 192.168.112.130:27017

查看数据库

查看当前所在数据库。

语法:

show dbs

查看所有数据库。

实例:

testrs:PRIMARY> show dbsadmin  0.000GBlocal  0.000GBtestrs:PRIMARY>

创建(切换)数据库

切换到所需数据库或创建新数据库。

语法:

use database_name

实例:

testrs:PRIMARY> use yunweijiaswitched to db yunweijiatestrs:PRIMARY>

创建数据库时需要先插入数据,新数据库才会显示出来。

删除数据库

删除当前数据库时请确保在正确的数据库中执行操作。

语法:

db.dropDatabase()

实例:

testrs:PRIMARY> show dbsadmin  0.000GBlocal  0.000GByunweijia  0.000GBtestrs:PRIMARY>testrs:PRIMARY> use yunweijiaswitched to db yunweijiatestrs:PRIMARY> db.dropDatabase(){ "dropped" : "yunweijia", "ok" : 1 }testrs:PRIMARY> show dbsadmin  0.000GBlocal  0.000GBtestrs:PRIMARY>

创建集合

创建或切换到指定集合。

语法:

db.createCollection("collection_name", options)

选项说明:

  • capped:如果为true,表示固定大小集合,达到最大值后自动覆盖旧文档。
  • size:固定集合的最大值(字节数)。
  • max:固定集合中包含文档的最大数量。

实例:

testrs:PRIMARY> use yunweijiaswitched to db yunweijiatestrs:PRIMARY> db.createCollection("ceshi"){ "ok" : 1 }testrs:PRIMARY>

在插入数据时,MongoDB会自动创建集合。

删除集合

删除指定集合。

语法:

db.collection.drop()

实例:

testrs:PRIMARY> use yunweijiaswitched to db yunweijiatestrs:PRIMARY> show collectionsceshijiersuneryunweijiatestrs:PRIMARY>testrs:PRIMARY> db.jier.drop()truetestrs:PRIMARY> show collectionsceshisuneryunweijiatestrs:PRIMARY>

插入文档

将文档插入集合中。

语法:

db.collection.insert(document)

插入数据时,_id字段会自动生成。

实例:

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>

也可以通过变量定义文档后插入:

document = {    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.collection.update(query, update, options)

参数说明:

  • query:更新的条件。
  • update:更新的内容。
  • options:包含upsertmultiwriteConcern等参数。

实例:

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>

如果需要更新多条匹配文档,可以设置multi为true。

删除文档

删除集合中的文档。

语法:

db.collection.remove(query, options)

参数说明:

  • query:删除的条件。
  • options:包含justOnewriteConcern等参数。

实例:

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(){    "_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>testrs:PRIMARY> db.col.remove({'title':'MongoDB 基础操作'})WriteResult({ "nRemoved" : 2 })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>

查询文档

查询集合中的文档。

语法:

db.collection.find(query, projection)

参数说明:

  • query:查询条件。
  • projection:指定返回的字段。

实例:

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的基础操作,包括数据库管理、集合操作、文档插入、更新、删除及查询等功能。通过这些命令,您可以高效地管理和操作MongoDB数据库,满足日常的开发需求。

转载地址:http://tiffk.baihongyu.com/

你可能感兴趣的文章
malloc和定位new表达式
查看>>
MAMP无法正常启动,错误提示The built-in Apache is active
查看>>
MangoDB4.0版本的安装与配置
查看>>
Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
查看>>
Manjaro 24.2 “Yonada” 发布:尖端功能与精美界面再度进化
查看>>
map 函数返回的列表在使用一次后消失
查看>>
Map 遍历取值及jstl的取值
查看>>
Mapbox GL示例教程【目录】-- 已有80篇
查看>>
mapping文件目录生成修改
查看>>
MapReduce程序依赖的jar包
查看>>
MapStruct 映射过程中忽略某个字段
查看>>
map和bean的相互转换
查看>>
Map的深浅拷贝的探究
查看>>
Map集合循环遍历的几种方式
查看>>
mariadb multi-source replication(mariadb多主复制)
查看>>
MariaDB的简单使用
查看>>
Mark Mind:下一代思维导图编辑器
查看>>
Markdown —— 背景色
查看>>
MaterialForm对tab页进行隐藏
查看>>
materialTabControl1_SelectedIndexChanged的使用
查看>>