mongodb的命令:

本文详细介绍了MongoDB的相关命令,包括启动服务、数据库管理、集合操作、文档的增删改查、索引创建、聚合操作以及监控工具mongostat和mongotop的使用。内容涵盖基本操作如查看数据库、创建索引,到高级特性如聚合管道、数据类型的比较以及监控数据库性能的方法。
摘要由CSDN通过智能技术生成

 

启动服务

./bin/mongod -f ./conf/mongod.conf

启动 命令行

./bin/mongo localhost:12345

> db
test
> use local
switched to db local
> db.createCollection("mycoll", {capped:true, size:100000})
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(0, 0),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(0, 0)
}

 初始化:

rs.initiate()
{
	"info2" : "no configuration specified. Using a default configuration for the set",
	"me" : "localhost:12345",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1599893065, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1599893065, 1)
}

数据库

查看数据库

rs0:SECONDARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
db.col.findOne()
null

 创建一个db:’

rs0:PRIMARY> use jinghang
switched to db jinghang
rs0:PRIMARY> db
jinghang
rs0:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
rs0:PRIMARY> db.jinghang.insert({"name":"景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show dbs
admin     0.000GB
config    0.000GB
jinghang  0.000GB
local     0.000GB

删除db:

rs0:PRIMARY> show dbs
admin     0.000GB
config    0.000GB
jinghang  0.000GB
local     0.000GB
rs0:PRIMARY> use jinghang
switched to db jinghang
rs0:PRIMARY> db.dropDatabase()
{
	"dropped" : "jinghang",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1599901507, 2),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1599901507, 2)
}
rs0:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

创建集合:

rs0:PRIMARY> show dbs
admin     0.000GB
config    0.000GB
jinghang  0.000GB
local     0.000GB
rs0:PRIMARY>  use test
switched to db test
rs0:PRIMARY> db.createCollection("jinghang")
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1599901881, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1599901881, 1)
}
rs0:PRIMARY> show collections
jinghang
rs0:PRIMARY> db.createCollection("mycol", { capped : true, autoIndexId : true, size : 
...    6142800, max : 10000 } )
{
	"note" : "The autoIndexId option is deprecated and will be removed in a future release",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1599901909, 2),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1599901909, 2)
}
rs0:PRIMARY> db.mycol2.insert({"name" : "景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show collections
jinghang
mycol
mycol2
rs0:PRIMARY> 

删除集合:

rs0:PRIMARY> use mydb
switched to db mydb
rs0:PRIMARY> show collections
rs0:PRIMARY> db.mycol2.insert({"name" : "景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.mycol3.insert({"name" : "景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.mycol4.insert({"name" : "景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show collections
mycol2
mycol3
mycol4
rs0:PRIMARY> db.mycol2.drop()
true
rs0:PRIMARY> show collections
mycol3
mycol4
rs0:PRIMARY> 

插入文档:

rs0:PRIMARY> db.col.insert({title: 'MongoDB 教程', 
...     description: 'MongoDB 是一个 Nosql 数据库',
...     by: '景航教程',
...     url: 'http://www.jinghang.com',
...     tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show collections
col
mycol3
mycol4
rs0:PRIMARY> db.col.find()
{ "_id" : ObjectId("5f5c92e3da33b31d5e0f590b"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "景航教程", "url" : "http://www.jinghang.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
rs0:PRIMARY> db.col.portI().find()
uncaught exception: TypeError: db.col.portI is not a function :
@(shell):1:1
rs0:PRIMARY> document=({title: 'MongoDB 教程', 
...     description: 'MongoDB 是一个 Nosql 数据库',
...     by: '景航教程',
...     url: 'http://www.jinghang.com',
...     tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100});
{
	"title" : "MongoDB 教程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
rs0:PRIMARY> db.col.insert(document)
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> 

更新文档:

rs0:PRIMARY> db.col.insert({
...     title: 'MongoDB 教程', 
...     description: 'MongoDB 是一个 Nosql 数据库',
...     by: '景航教程',
...     url: 'http://www.jinghang.com',
...     tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB教'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
rs0:PRIMARY> db.col.find().pretty()
{
	"_id" : ObjectId("5f5c92e3da33b31d5e0f590b"),
	"title" : "MongoDB教",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c9340da33b31d5e0f590c"),
	"title" : "MongoDB 教程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c93c6da33b31d5e0f590d"),
	"title" : "MongoDB 教程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
rs0:PRIMARY> db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB 程'}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
rs0:PRIMARY> db.col.find().pretty()
{
	"_id" : ObjectId("5f5c92e3da33b31d5e0f590b"),
	"title" : "MongoDB教",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c9340da33b31d5e0f590c"),
	"title" : "MongoDB 程",
	"d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值